observe fixes

Dependencies:   nsdl_lib Nanostack_lib

Fork of mbedEndpointNetwork by Michael Koster

Committer:
michaeljkoster
Date:
Thu Apr 23 02:57:56 2015 +0000
Revision:
10:da76961ba3e9
Parent:
4:2c8eeaf31699
use prebuilt nsdl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:2a5a48a8b4d4 1 /**
ansond 0:2a5a48a8b4d4 2 * @file network_stubs.cpp
ansond 0:2a5a48a8b4d4 3 * @brief mbed Endpoint network stubs implementation (6LowPAN)
ansond 0:2a5a48a8b4d4 4 * @author Doug Anson
ansond 0:2a5a48a8b4d4 5 * @version 1.0
ansond 0:2a5a48a8b4d4 6 * @see
ansond 0:2a5a48a8b4d4 7 *
ansond 0:2a5a48a8b4d4 8 * Copyright (c) 2014
ansond 0:2a5a48a8b4d4 9 *
ansond 0:2a5a48a8b4d4 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 0:2a5a48a8b4d4 11 * you may not use this file except in compliance with the License.
ansond 0:2a5a48a8b4d4 12 * You may obtain a copy of the License at
ansond 0:2a5a48a8b4d4 13 *
ansond 0:2a5a48a8b4d4 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 0:2a5a48a8b4d4 15 *
ansond 0:2a5a48a8b4d4 16 * Unless required by applicable law or agreed to in writing, software
ansond 0:2a5a48a8b4d4 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 0:2a5a48a8b4d4 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 0:2a5a48a8b4d4 19 * See the License for the specific language governing permissions and
ansond 0:2a5a48a8b4d4 20 * limitations under the License.
ansond 0:2a5a48a8b4d4 21 */
ansond 0:2a5a48a8b4d4 22
ansond 0:2a5a48a8b4d4 23 #include "network_stubs.h"
ansond 0:2a5a48a8b4d4 24
ansond 0:2a5a48a8b4d4 25 extern "C" {
ansond 0:2a5a48a8b4d4 26
ansond 0:2a5a48a8b4d4 27 // Tasklet ID..
ansond 0:2a5a48a8b4d4 28 int main_tasklet_id = -1;
ansond 0:2a5a48a8b4d4 29
ansond 3:5711cc2c6253 30 // randomize the MAC address
ansond 3:5711cc2c6253 31 void randomize_mac_address()
ansond 3:5711cc2c6253 32 {
ansond 3:5711cc2c6253 33 // just extern it for now
ansond 3:5711cc2c6253 34 extern uint8_t app_MAC_address[NODE_MAC_ADDRESS_LENGTH];
ansond 3:5711cc2c6253 35 extern uint8_t endpoint_name[NODE_NAME_LENGTH];
ansond 3:5711cc2c6253 36
ansond 3:5711cc2c6253 37 // seed (weak)
ansond 3:5711cc2c6253 38 srand(time(NULL));
ansond 3:5711cc2c6253 39
ansond 3:5711cc2c6253 40 // rand() with some added variance from endpoint name
ansond 3:5711cc2c6253 41 for(int i=0;i<NODE_MAC_ADDRESS_LENGTH;++i) app_MAC_address[i] = (rand()*endpoint_name[i])%256;
ansond 3:5711cc2c6253 42
ansond 3:5711cc2c6253 43 // display
ansond 3:5711cc2c6253 44 std::printf("MAC Address: 0x%.2x::0x%.2x::0x%.2x::0x%.2x::0x%.2x::0x%.2x::0x%.2x::0x%.2x\r\n",
ansond 3:5711cc2c6253 45 app_MAC_address[0],app_MAC_address[1],app_MAC_address[2],app_MAC_address[3],
ansond 3:5711cc2c6253 46 app_MAC_address[4],app_MAC_address[5],app_MAC_address[6],app_MAC_address[7]);
ansond 3:5711cc2c6253 47 }
ansond 3:5711cc2c6253 48
ansond 0:2a5a48a8b4d4 49 // Nanostack_6LowPAN tasklet main handler function...
ansond 0:2a5a48a8b4d4 50 extern "C" void tasklet_main(void);
ansond 0:2a5a48a8b4d4 51
ansond 0:2a5a48a8b4d4 52 // plumb out the network
ansond 4:2c8eeaf31699 53 void net_stubs_pre_plumb_network(bool canActAsRouterNode)
ansond 0:2a5a48a8b4d4 54 {
ansond 3:5711cc2c6253 55 // randomize the MAC address
ansond 3:5711cc2c6253 56 randomize_mac_address();
ansond 3:5711cc2c6253 57
ansond 0:2a5a48a8b4d4 58 // call init_network()
ansond 0:2a5a48a8b4d4 59 init_network(canActAsRouterNode);
ansond 0:2a5a48a8b4d4 60 }
ansond 0:2a5a48a8b4d4 61
ansond 4:2c8eeaf31699 62 // called after the endpoint is configured...
ansond 4:2c8eeaf31699 63 void net_stubs_post_plumb_network(void)
ansond 4:2c8eeaf31699 64 {
ansond 4:2c8eeaf31699 65 // not used - mesh network is already setup...
ansond 4:2c8eeaf31699 66 ;
ansond 4:2c8eeaf31699 67 }
ansond 4:2c8eeaf31699 68
ansond 0:2a5a48a8b4d4 69 // create a suitable main event loop for this specific network
ansond 0:2a5a48a8b4d4 70 void net_stubs_create_main_loop(void)
ansond 0:2a5a48a8b4d4 71 {
ansond 0:2a5a48a8b4d4 72 // tasklet creation...
ansond 0:2a5a48a8b4d4 73 DBG("net_stubs_begin_main_loop: creating tasklet for main loop...\r\n");
ansond 0:2a5a48a8b4d4 74 main_tasklet_id = arm_ns_tasklet_create(&tasklet_main);
ansond 0:2a5a48a8b4d4 75 if(main_tasklet_id < 0) {
ansond 0:2a5a48a8b4d4 76 //Tasklet cerate fail
ansond 0:2a5a48a8b4d4 77 DBG("net_stubs_begin_main_loop: Tasklet creation failed...\r\n");
ansond 0:2a5a48a8b4d4 78 return;
ansond 0:2a5a48a8b4d4 79 }
ansond 0:2a5a48a8b4d4 80 }
ansond 0:2a5a48a8b4d4 81
ansond 0:2a5a48a8b4d4 82 // register the endpoint
ansond 0:2a5a48a8b4d4 83 void net_stubs_register_endpoint(void)
ansond 0:2a5a48a8b4d4 84 {
ansond 0:2a5a48a8b4d4 85 // call NSP_registration()
ansond 0:2a5a48a8b4d4 86 NSP_registration();
ansond 0:2a5a48a8b4d4 87 }
ansond 0:2a5a48a8b4d4 88
ansond 0:2a5a48a8b4d4 89 // begin the main loop for processing network events
ansond 0:2a5a48a8b4d4 90 void net_stubs_begin_main_loop(void)
ansond 0:2a5a48a8b4d4 91 {
ansond 0:2a5a48a8b4d4 92 // start event dispatching
ansond 0:2a5a48a8b4d4 93 DBG("net_stubs_begin_main_loop: Beginning event dispatch...\r\n");
ansond 0:2a5a48a8b4d4 94 event_dispatch();
ansond 0:2a5a48a8b4d4 95 return;
ansond 0:2a5a48a8b4d4 96 }
ansond 0:2a5a48a8b4d4 97
ansond 0:2a5a48a8b4d4 98 }