/* * @brief ... * @details ... */ bool radioRecvStartI(RadioDriver *radio) { consoleDevel("radioRecvStartS() start\r\n"); if (radio_lld_receive_start(radio) == true) { radio->state = RADIO_RX; consoleDevel("radioRecvStartS() end\r\n"); return true; } else { radio->state = RADIO_ERROR; consoleWarn("radioRecvStartS() failed\r\n"); return false; } }
/* * @brief ... * @details ... */ bool radioIdleI(RadioDriver *radio) { consoleDevel("radioIdleI() start\r\n"); if (radio_lld_idle(radio) == true) { radio->state = RADIO_IDLE; consoleDevel("radioIdleI() end\r\n"); return true; } else { radio->state = RADIO_ERROR; consoleWarn("radioIdleI() failed\r\n"); return false; } }
/* * @brief ... * @details ... */ bool radioSendStartI(RadioDriver *radio, radio_packet_t *packet) { consoleDevel("radioSendStartS() start\r\n"); memcpy(&radio->packet, packet, sizeof(radio_packet_t)); if (radio_lld_send_start(radio) == true) { radio->state = RADIO_TX; consoleDevel("radioSendStartS() end\r\n"); return true; } else { radio->state = RADIO_ERROR; consoleWarn("radioSendStartS() failed\r\n"); return false; } }
/* * @brief ... * @details ... */ bool radioSendStart(RadioDriver *radio, radio_packet_t *packet) { // bool ret; // consoleDevel("radioSendStart() start\r\n"); if (ithacaLockTimeout(&radio->lock, 5) == false) { consoleWarn("radioSendStart() failed\r\n"); return false; } ret = radioSendStartI(radio, packet); ithacaUnlock(&radio->lock); consoleDevel("radioSendStart() end == %s\r\n", ret ? "true" : "false"); return ret; }
/* * @brief ... * @details ... */ bool radioIdle(RadioDriver *radio) { // bool ret; // consoleDevel("radioIdle() start\r\n"); if (ithacaLockTimeout(&radio->lock, 5) == false) { consoleWarn("radioIdle() failed\r\n"); return false; } ret = radioIdleI(radio); ithacaUnlock(&radio->lock); consoleDevel("radioIdle() end == %s\r\n", ret ? "true" : "false"); return ret; }
/* * @brief ... * @details ... */ bool radio_lld_send_start(RadioDriver *radio) { bool ret_val = false; consoleDevel("radio_lld_send_start() start, lld_type == %d\r\n", radio->config->lld_type); if (radio->config->timeout != 0) { radio->timeout = chTimeNow() + MS2ST(radio->config->timeout); } else { radio->timeout = 0; } switch (radio->config->lld_type) { case RADIO_LLD_TYPE_RFM12B: rfm12b_lld_send_start(radio); ret_val = true; break; } if (ret_val == true) { consoleDevel("radio_lld_send_start() end\r\n"); } else { consoleWarn("radio_lld_send_start() failed\r\n"); } return ret_val; }
/* * @brief Radio thread. * @details This thread performs switch between different states of radio module. It also executes defined callbacks. */ static void radio_thread(void *radio_driver) { // RadioDriver *radio = (RadioDriver *)radio_driver; radio_callback_t cb = NULL; // if (radio->config->name != NULL) { chRegSetThreadName(radio->config->name); } consoleDebug("started\r\n"); // while (true) { consoleDevel("loop start\r\n"); cb = NULL; if (ithacaLock(&radio->lock) == true) { switch (radio->state) { case RADIO_UNINIT: consoleDebug("RADIO_UNINIT\r\n"); if (radio_lld_init(radio) == true) { radio->state = RADIO_STOP; consoleDevel("init ok\r\n"); } else { consoleDebug("init failed\r\n"); } break; case RADIO_STOP: consoleDebug("RADIO_STOP\r\n"); if (radioIdleI(radio) == true) { consoleDevel("stop -> idle ok\r\n"); } else { consoleWarn("stop -> idle failed\r\n"); radio->state = RADIO_UNINIT; } break; case RADIO_IDLE: consoleDebug("RADIO_IDLE\r\n"); if (radio->config->idle_cb != NULL) { cb = radio->config->idle_cb; } else { consoleDevel("idle_cb == NULL\r\n"); } break; case RADIO_RX: consoleDebug("RADIO_RX\r\n"); if ((radio_lld_is_error(radio) == true) || (radio_lld_is_timeout_expired(radio) == true)) { if (radio->config->recv_error_cb != NULL) { cb = radio->config->recv_error_cb; } else { radio->state = RADIO_ERROR; consoleDevel("recv_error_cb == NULL\r\n"); } consoleWarn("rx failed\r\n"); } else if (radio_lld_receive_is_completed(radio) == true) { if (radio->config->recv_done_cb != NULL) { cb = radio->config->recv_done_cb; } else { radio->state = RADIO_IDLE; consoleDevel("recv_done_cb == NULL\r\n"); } consoleDebug("rx completed ok\r\n"); } else { consoleDevel("rx in progress\r\n"); } break; case RADIO_TX: consoleDebug("RADIO_TX\r\n"); if ((radio_lld_is_error(radio) == true) || (radio_lld_is_timeout_expired(radio) == true)) { if (radio->config->send_error_cb != NULL) { cb = radio->config->send_error_cb; } else { radio->state = RADIO_ERROR; consoleDevel("send_error_cb == NULL\r\n"); } consoleWarn("tx failed\r\n"); } else if (radio_lld_send_is_completed(radio) == true) { if (radio->config->send_done_cb != NULL) { cb = radio->config->send_done_cb; } else { radio->state = RADIO_IDLE; } consoleDebug("tx completed ok\r\n"); } else { consoleDevel("tx in progress\r\n"); } break; case RADIO_ERROR: consoleDebug("RADIO_ERROR\r\n"); if (radioIdleI(radio) == true) { consoleDevel("error -> idle ok\r\n"); } else { consoleWarn("eror -> idle failed\r\n"); radio->state = RADIO_UNINIT; } break; } if (cb != NULL ) { consoleDevel("callback execute\r\n"); cb(radio); } ithacaUnlock(&radio->lock); } consoleDevel("loop yield\r\n"); chThdYield(); } }
int clientConnect(int argc, char **argv) { hostent *ptr_client; /* Client infos */ hostent *ptr_host; /* Host infos */ char *prog; /* Program Name */ const char *hostc; /* Server address char* */ in_addr host; /* Server address */ char login[LOGIN_SIZE]; /* User name */ char pass[PASSWORD_MAX]; /* User password */ int port; /* Port of the remote machine */ bool linked = False; /* If the linbk with server is etablished */ int try = 1; /* Number of connecting try */ int try_Max = MAX_CONNECTION_TRY; /* Maximum of try to connect */ int askCo = 0; /* Result of the connection try */ STATE = False; /* Set client status to disconnected */ int errorN = 0; /* Error numlber */ struct timeval timeout; /* Timeout for read and write */ timeout.tv_sec = RS_TIMEOUT; timeout.tv_usec = 0; if (argc < 5) { if(argc>0) { prog = argv[0]; } else { prog = "client"; } sprintf(logC,"%s <server_address> <server_port> <user_name> <user_password>\n",prog); consoleUsage(logC); memset(&logC,0,strlen(logC)); exit(1); } prog = argv[0]; hostc = argv[1]; inet_aton(hostc, &host); port = atoi(argv[2]); strcpy(login,argv[3]); if (strlen(login) > (LOGIN_SIZE-1)){ // 'Cause of '\0' /* Error 13: Login size too long (17 char max) */ consoleErrorLog(13); exit(1); } strcpy(pass,argv[4]); if (strlen(pass) > (PASSWORD_MAX-1) || strlen(pass) < (PASSWORD_MIN)){ // 'Cause of '\0' /* Error 14: Password size incorrect (Between 7 and 19) */ consoleErrorLog(14); exit(1); } int argN = 5; /* while(argN<argc) { char tmp[strlen(msg)+strlen(argv[argN])+1]; strcpy(tmp,msg); strcat(tmp," "); strcat(tmp,argv[argN]); strcpy(msg,tmp); argN++; } */ char machine[NAME_SIZE+1]; gethostname(machine,NAME_SIZE); if((ptr_client = gethostbyname(machine)) == NULL) { consoleError("Can not find the machine address"); return errno; } /* character by character copy of ptr_client informations to client address */ bcopy((char*)ptr_client->h_addr, (char*)&client_address.sin_addr,ptr_client->h_length); client_address.sin_family = AF_INET; /* Use a new port number */ client_address.sin_port = htons(port); if((ptr_host = gethostbyname(hostc)) == NULL) { consoleError("Can not find the server from its address"); return errno; } /* character by character copy of ptr_host informations to local address */ bcopy((char*)ptr_host->h_addr, (char*)&server_address.sin_addr,ptr_host->h_length); server_address.sin_family = AF_INET; /* Use a new port number */ server_address.sin_port = htons(port); /* Creation of the socket */ if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) <0){ consoleError("Unable to create socket connection with the server"); return errno; } if (setsockopt (socket_descriptor, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0) { consoleWarn("Set socket receive options failed\n"); } if (setsockopt (socket_descriptor, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0) { consoleWarn("Set socket send options failed\n"); } while(!STATE&&try<=try_Max) { /* Try to connect to the server with infos in server_address */ if(!linked&&(connect(socket_descriptor, (sockaddr*)(&server_address), sizeof(sockaddr_in))) < 0) { consoleError("Unable to connect to server"); errorN=errno; } else { if(!linked) { consoleLog("Connection established with the server\n"); errorN=0; } linked=True; askCo=askConnection(server_address.sin_addr,login,pass,SERVER_ADMIN_NAME); if(askCo==0&&STATE) { try=0; } else { /* Server is full, we can try until someone disconnect */ if(askCo==0) { try_Max=100; } else { // Wrong password || Login not allowed || Login already used if(askCo==3||askCo==2||askCo==1) { exit(0); } try_Max=MAX_CONNECTION_TRY; } } } if(!STATE) { sprintf(errorC,"Attempt (#%d) to connect to server failed [with error #%d]\n",try++,askCo); consoleError(errorC); memset(&errorC,0,strlen(errorC)); if(askCo==14||askCo==22) { errorN=askCo; close(socket_descriptor); return clientConnect(argc,argv); } if(try<try_Max) { consoleLog("Retry to connect in 3 seconds...\n"); sleep(3); } }