NETWORK_STATE network_check_set_dhcp( void ) { int ready = 0; time_t tm; SecureSOHO_LAN lan; NETWORK_STATE state = network_get_state(); switch (state) { case NETWORK_STATE_WIRELESS_CONNECTING: tm = time(NULL) - network_task.start_time; if ( tm >= network_task.timeout ) { fprintf(stderr, "Wireless timeout\n"); stop_wireless_thread(); network_set_state(NETWORK_STATE_WIRELESS_TIMEOUT); } break; case NETWORK_STATE_REQUESTING_IP: case NETWORK_STATE_DONE: if (securesoho_lan_get(&lan)) { fprintf(stderr, "##%s;%d, failed to securesoho_lan_get\n", __FUNCTION__, __LINE__); return 0; } if (lan.lan_type == LAN_FIXEDIP) { ready = 1; break; } if ( exist(DHCP_FLAG_FILE) ) { fprintf(stderr, "##%s;%d, dhcp ip got!\n", __FUNCTION__, __LINE__); unlink("/tmp/ftp_mode"); unlink(DHCP_FLAG_FILE); unlink(AUTO_FLAG_FILE); network_save_dhcpinfo(lan.lan_ifname); ready = 1; } else if (exist(AUTO_FLAG_FILE) ) { fprintf(stderr, "##%s;%d, auto ip got!\n", __FUNCTION__, __LINE__); unlink(AUTO_FLAG_FILE); network_save_autoinfo(lan.lan_ifname); ready = 1; } if (ready) { network_if_conf(); network_set_state(NETWORK_STATE_DONE); } default: break; } return network_task.state; }
int network_update_wireless_state(int success) { time_t tm = time(NULL) - network_task.start_time; char lan_type[64]; if (success) { securesoho_string_get( "LAN_TYPE", lan_type); if (lan_type[0] == 'd') { if (tm >= network_task.timeout) { network_set_state(NETWORK_STATE_WIRELESS_TIMEOUT); printf("%s:%d, wireless timeout\n", __FUNCTION__, __LINE__); } else { network_ifconf_dhcp( network_task.timeout - tm ); network_set_state(NETWORK_STATE_REQUESTING_IP); } } else { network_ifconf_static(); network_set_state(NETWORK_STATE_DONE); } } else { network_set_state(NETWORK_STATE_WIRELESS_TIMEOUT); } sem_wait(&network_task.lock); network_task.pid = 0; sem_post(&network_task.lock); return 0; }
/* This function intializes network parameters */ void network_init(uint16_t uid) { uprintf("\r\n##########################################\r\n"); uprintf("Network init called with UID = %d\r\n",uid); deviceType = get_device_type(uid); if(deviceType == DEVICE_TYPE_COORDINATOR) { uprintf("I'm a Cooridinator\r\n"); } if(deviceType == DEVICE_TYPE_ROUTER) { uprintf("I'm a Router\r\n"); } bool rx_on_idle = true; CS_WriteParameter(CS_RX_ON_WHEN_IDLE_ID, &rx_on_idle); /*False = random ID */ /* True = static short address */ bool unique_addr = true; CS_WriteParameter(CS_NWK_UNIQUE_ADDR_ID,&unique_addr); ExtAddr_t ownExtAddr = 0x1LL; CS_WriteParameter(CS_UID_ID, &ownExtAddr); CS_WriteParameter(CS_NWK_ADDR_ID, &uid); // Set the deviceType value to Config Server CS_WriteParameter(CS_DEVICE_TYPE_ID, &deviceType); network_set_state(APP_NETWORK_JOIN_REQUEST); }
/* brief Handler of aps data sent confirmation */ void APS_DataConf(APS_DataConf_t* confInfo) { static int retryCounter = 0; if (APS_SUCCESS_STATUS != confInfo->status) { retryCounter++; uprintf("!!! Last transfert was failed. Reset it (retry = %d)\r\n",retryCounter); if (MAX_RETRIES_BEFORE_REJOIN == retryCounter) { uprintf("!!! Too much failed transfert. You are now disconnected from the network %d\r\n",retryCounter); retryCounter = 0; network_leave(); network_set_transmission_state(APP_DATA_TRANSMISSION_READY_STATE); network_set_state(APP_NETWORK_JOIN_REQUEST); } else { /* Data not delivered, resend.*/ network_set_transmission_state(APP_DATA_TRANSMISSION_SENDING_STATE); network_start_transmission(); } } else { network_set_transmission_state(APP_DATA_TRANSMISSION_READY_STATE); network_specific_DataConfcallback(); retryCounter = 0; } }
/* Update network status event handler */ void ZDO_MgmtNwkUpdateNotf(ZDO_MgmtNwkUpdateNotf_t *nwkParams) { switch (nwkParams->status) { case ZDO_NETWORK_STARTED_STATUS: break; case ZDO_NETWORK_LOST_STATUS: network_leave(); network_set_state(APP_NETWORK_JOIN_REQUEST); break; case ZDO_NWK_UPDATE_STATUS: break; case ZDO_CHILD_JOINED_STATUS: uprintf("Child %d joined\r\n",nwkParams->childInfo.shortAddr); uprintf(" > RSSI = %d dBm (-91 dBm to -7 dBm)\r\n",(int8_t)network_get_rssi(nwkParams->childInfo.shortAddr)); uprintf(" > LQI = %d (0 to 255)\r\n",(uint8_t)network_get_lqi(nwkParams->childInfo.shortAddr)); /* if dongle_zigbit then aciivate uart over zb */ if(get_device_functionnality(nwkParams->childInfo.shortAddr) == ZIGBIT_DONGLE) serial_over_zigbit_start(nwkParams->childInfo.shortAddr); break; case ZDO_CHILD_REMOVED_STATUS: uprintf("Child %d quit\r\n",nwkParams->childInfo.shortAddr); /* if dongle_zigbit then deaciivate uart over zb */ if(get_device_functionnality(nwkParams->childInfo.shortAddr) == ZIGBIT_DONGLE) serial_over_zigbit_stop(); break; default: uprintf("Network status = 0x%x\r\n",nwkParams->status); break; } }
static void network_task_init( void ) { memset(&network_task, 0, sizeof(struct network_task_t)); network_task.timeout = DEFAULT_TIMEOUT_SECS; network_task.pid = 0; sem_init(&network_task.lock, 0, 1); network_task.thread_exit = 0; network_set_state(NETWORK_STATE_INIT); }
int network_restart(int timeout) { int dhcp = 0; char lan_type[64]; char lif[16]; if (first_init_flag) { first_init_flag = 0; network_task_init(); } if (timeout > 0 ) network_task.timeout = timeout; else network_task.timeout = DEFAULT_TIMEOUT_SECS; network_task.start_time = time(NULL); network_reset(); securesoho_lif_get(lif); securesoho_string_get( "LAN_TYPE", lan_type); dhcp = (lan_type[0] == 'd' ? 1 : 0); if ( !strcmp(lif, WLAN_PORT) ) { printf("network_restart:start wireless thread\n"); start_wireless_thread(); network_set_state(NETWORK_STATE_WIRELESS_CONNECTING); } else { if (dhcp) { network_ifconf_dhcp(network_task.timeout); network_set_state(NETWORK_STATE_REQUESTING_IP); } else { network_ifconf_static(); network_set_state(NETWORK_STATE_DONE); } } return dhcp; }
void APL_TaskHandler(void) { switch (network_get_state()) { case APP_INITIAL_STATE: /* Init Led */ init_led(); /* Init Serial Interface for debug */ initSerialInterface(); uid = get_uid(); /* Init network */ uid = 2; network_init(uid); network_set_state(APP_NETWORK_JOIN_REQUEST); break; case APP_NETWORK_JOIN_REQUEST: /* Activate the network status led blink */ led_start_blink(); /* St art network */ network_start(); network_set_state(APP_NETWORK_JOINING_STATE); case APP_NETWORK_JOINING_STATE: break; case APP_NETWORK_LEAVING_STATE: break; case APP_NETWORK_JOINED_STATE: led_stop_blink(); break; default: break; } SYS_PostTask(APL_TASK_ID); }
static int network_reset( void ) { stop_dhcp_client(); stop_wpa_client(); stop_wireless_thread(); do_cmd(IFCONFIG, LAN_PORT, "0.0.0.0", "down", NULL); network_set_state(NETWORK_STATE_INIT); return 0; }
/* ZDO_StartNetwork primitive confirmation callback */ void ZDO_StartNetworkConf(ZDO_StartNetworkConf_t* confirmInfo) { if (confirmInfo->status == ZDO_SUCCESS_STATUS) { network_set_state(APP_NETWORK_JOINED_STATE); /* Set application endpoint properties */ endpointParams.simpleDescriptor = &simpleDescriptor; endpointParams.APS_DataInd = APS_DataIndication; /* Register endpoint */ APS_RegisterEndpointReq(&endpointParams); /* Update status : ready for transfert */ network_set_transmission_state(APP_DATA_TRANSMISSION_READY_STATE); /* Display useful informations */ uprintf("Connection etablished\r\n"); uprintf(" > Parent : %d\r\n'",confirmInfo->parentAddr); uprintf(" > Channel : %d\r\n",confirmInfo->activeChannel); uprintf(" > Short Address : %d\r\n",confirmInfo->shortAddr); uprintf(" > PANid : 0x%x\r\n",(uint16_t)confirmInfo->PANId); uprintf(" > RSSI = %d dBm (-91 dBm to -7 dBm)\r\n",(int8_t)network_get_rssi(confirmInfo->parentAddr)); uprintf(" > LQI = %d (0 to 255)\r\n",(uint8_t)network_get_lqi(confirmInfo->parentAddr)); uprintf("##########################################\r\n"); } else { uprintf("Connection failed ! returned status = 0x%x\n\r",confirmInfo->status); uprintf("##########################################\r\n"); /* if communication is failed, try to rejoin */ network_set_state(APP_NETWORK_JOIN_REQUEST); } }