void Console::keyPress(MyGUI::Widget* _sender, MyGUI::KeyCode key, MyGUI::Char _char) { if( key == MyGUI::KeyCode::Tab) { std::vector<std::string> matches; listNames(); std::string oldCaption = mCommandLine->getCaption(); std::string newCaption = complete( mCommandLine->getOnlyText(), matches ); mCommandLine->setCaption(newCaption); // List candidates if repeatedly pressing tab if (oldCaption == newCaption && !matches.empty()) { int i = 0; printOK(""); for(std::vector<std::string>::iterator it=matches.begin(); it < matches.end(); ++it,++i ) { printOK( *it ); if( i == 50 ) break; } } } if(mCommandHistory.empty()) return; // Traverse history with up and down arrows if(key == MyGUI::KeyCode::ArrowUp) { // If the user was editing a string, store it for later if(mCurrent == mCommandHistory.end()) mEditString = mCommandLine->getOnlyText(); if(mCurrent != mCommandHistory.begin()) { --mCurrent; mCommandLine->setCaption(*mCurrent); } } else if(key == MyGUI::KeyCode::ArrowDown) { if(mCurrent != mCommandHistory.end()) { ++mCurrent; if(mCurrent != mCommandHistory.end()) mCommandLine->setCaption(*mCurrent); else // Restore the edit string mCommandLine->setCaption(mEditString); } } }
void test_single_thread_many_tasks() { //ignore ThreadPool* tp = tpCreate(1); tpInsertTask(tp,doMediumTaskWithPrint,NULL); tpInsertTask(tp,doMediumTaskWithPrint,NULL); AwesomeContainer con; con.awesomeNum = 3; con.awesomeString = "betty bought a bit of butter but the butter betty bought was bitter"; tpInsertTask(tp,awesomePrint,&con); tpInsertTask(tp,doMediumTaskWithPrint,NULL); tpInsertTask(tp,doMediumTaskWithPrint,NULL); int num1 = 10; tpInsertTask(tp,fibonaci,&num1); int num2 = 20; tpInsertTask(tp,fibonaci,&num2); int a=0; AwesomeContainer con2; con2.awesomeNum = 3; con2.awesomeString = "Whats your name? my name is Arnio"; tpInsertTask(tp,awesomePrint,&con2); tpInsertTask(tp,printingCannabisText,NULL); tpInsertTask(tp,doLongTaskWithPrint,&a); tpDestroy(tp,1); printOK(); printf(" \n"); }
// Argument msg is not a null-terminated string int msg_send(int callbackFd, char* destIpAddr, int destPort, const char* msg, int forceRediscovery) { if (callbackFd <= 0) { debug("API: callbackFd should be positive"); return -1; } if (destIpAddr == NULL) { debug("API: destIpAddr can't be NULL"); return -1; } if (destPort <= 0) { debug("API: destPort should be positive"); return -1; } if (msg == NULL) { debug("API: msg can't be NULL"); return -1; } //Building structure PayloadHdr ph; bzero(&ph, sizeof(ph)); if (destPort == SRV_PORT_NUMBER) { ph.msgType = CLIENT_MSG_TYPE; } else { ph.msgType = SRV_MSG_TYPE; } ph.forceRediscovery = forceRediscovery; if (strcmp(destIpAddr, "loc") == 0) { // for ODR this message means "Destination IP = local" ph.destIp = LOCAL_INET_IP; // inet_addr("0.1.2.3"); } else { ph.destIp = inet_addr(destIpAddr); } ph.destPort = destPort; //Initially int msgSpace = strlen(msg) + 1; bzero(ph.msg, msgSpace); strcpy(ph.msg, msg); printPayloadContents(&ph); // Packing and sending uint32_t bufLen = 0; void* packedBuf = packPayload(&ph, &bufLen); SockAddrUn addr = createSockAddrUn(ODR_UNIX_PATH); printf("Sending packed buffer, length=%u...", bufLen); int n; if ((n = sendto(callbackFd, packedBuf, bufLen, 0, (SA *)&addr, sizeof(addr))) == -1) { printFailed(); } else{ printOK(); PayloadHdr pp; unpackPayload(packedBuf, &pp); printPayloadContents(&pp); } free(packedBuf); return 0; }
void test_destroy_should_wait_for_tasks_2() { //ignore ThreadPool* tp = tpCreate(5); AwesomeContainer con; con.awesomeNum = 0; con.awesomeString = "DontCare"; // we use only the awesomeNum tpInsertTask(tp,doLongTask,NULL); tpInsertTask(tp,doLongTask,NULL); tpInsertTask(tp,doLongTask,NULL); tpInsertTask(tp,doLongTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doLongTask,&con); tpDestroy(tp,1); assert(con.awesomeNum==1); printOK(); printf(" \n"); }
int ckpmon_run(char *arg) { extern lua_State *L; gchar *pmon_input = NULL; char *pmon_buf = NULL; gsize length = 0; GRegex *regex; GMatchInfo *match_info; GError *err = NULL; printPrompt("PMON版本输入格式如下例:\"1.3.6\"\n请输入\n"); // get pmon spec version: pmon_input int len = getTableNumElement(L, "con", "PMONVER_LEN"); pmon_input = getNCharsPrompt("PMON版本条码", len, TRUE); if (pmon_input == NULL) { printPrompt("未输入\n"); return 1; } printPrompt("输入版本号为:"); printMsg(pmon_input); printMsg("\n"); // get pmon env version: pmon_env g_file_get_contents ("/proc/cmdline", &pmon_buf, &length, NULL); regex = g_regex_new (PMON_STR, G_REGEX_NO_AUTO_CAPTURE | G_REGEX_OPTIMIZE | G_REGEX_DUPNAMES, 0, &err); g_regex_match (regex, pmon_buf, 0, &match_info); gchar *pmon_named = g_match_info_fetch_named(match_info, "pmonver"); g_print ("%s\n", pmon_named); // cmp gchar *text_pmon_env = g_strdup_printf("本机的版本号[cmdline]为:%s\n", pmon_named); printPrompt(g_string_chunk_insert_const(text_chunk, text_pmon_env)); g_free(text_pmon_env); if (strcasecmp(pmon_input, (const char *)pmon_named)) { printNG("机器当前PMON版本号与标准不相符!\n"); return 1; } else { printOK("PMON版本号相符。\n"); } g_free(pmon_input); // TODO: here free g_strdup, but have not test g_free (pmon_named); g_match_info_free (match_info); g_regex_unref (regex); return 0; }
void test_agressive() { //ignore //repeat the same test many times to check for rare cases int i; for (i = 1; i <= 20; ++i) { aux_test_for_agressive(i); } printOK(); printf(" \n"); }
// Once this operation is still taking place no concurrent tpDestroy() are allowed on the same threadPool (PDF) void test_destroy_twice() { //ignore ThreadPool* tp = tpCreate(5); tpInsertTask(tp,doLongTask,NULL); tpInsertTask(tp,doLongTask,NULL); tpDestroy(tp,1); tpDestroy(tp,1); printOK(); printf(" \n"); }
int keyboard_run(char *arg) { printPrompt("进入键盘测试。\n"); gdk_threads_enter(); draw_kbd(); gdk_threads_leave(); if (kbd_result) { printNG("键盘测试失败\n"); } else { printOK("键盘测试通过\n"); } return kbd_result; }
void test_insert_task_after_destroy_2() { //ignore ThreadPool* tp = tpCreate(5); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doLongTask,NULL); tpDestroy(tp,0); tpInsertTask(tp,badfunction,NULL); printOK(); printf(" \n"); }
void test_thread_pool_sanity() { //ignore int i; ThreadPool* tp = tpCreate(3); for(i=0; i<3; ++i) { tpInsertTask(tp,hello,NULL); } tpDestroy(tp,1); printOK(); printf(" \n"); }
int webcam_run(char *arg) { printPrompt("进入摄像头测试\n请确认开启摄像头(Fn + F10)\n并根据实际情况选择'OK'或者'NG'\n"); gdk_threads_enter(); camera_work(); gdk_threads_leave(); if (cam_result) { printNG("摄像头测试失败\n"); } else { printOK("摄像头测试成功\n"); } return cam_result; }
void test_many_threads_single_task() { //ignore ThreadPool* tp = tpCreate(50); tpInsertTask(tp,doMediumTask,NULL); // AwesomeContainer con; // con.awesomeNum = 0; // con.awesomeString = NULL; // we use only the awesomeNum // tpInsertTask(tp,doLongTask,&con); tpDestroy(tp,1); //assert(con.awesomeNum==1); printOK(); printf(" \n"); }
void test_create_and_destroy() { int i = 0; //ignore printf("ok1\n"); ThreadPool* tp1 = tpCreate(3); printf("ok2\n"); tpDestroy(tp1,1); printf("ok3\n"); ThreadPool* tp2 = tpCreate(3); printf("ok4\n"); tpDestroy(tp2,0); printf("ok5\n"); /*//*/ printOK(); printf(" \n"); }
void test_destroy_should_not_wait_for_tasks() { //ignore ThreadPool* tp = tpCreate(4); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); tpInsertTask(tp,doMediumTask,NULL); // AwesomeContainer con; // con.awesomeNum = 0; // con.awesomeString = "DontCare"; // we use only the awesomeNum // tpInsertTask(tp,doMediumTaskWithPrint,&con); // tpInsertTask(tp,doMediumTaskWithPrint,&con); // tpInsertTask(tp,doMediumTaskWithPrint,&con); tpDestroy(tp,0); printOK(); printf(" \n"); }
int msg_recv(int sockfd, char* msg, char* srcIpAddr, int* srcPort) { fd_set set; int maxfd; struct timeval tv; PayloadHdr ph; char* buf = malloc(ETHFR_MAXDATA_LEN); for(;;){ tv.tv_sec = 10; tv.tv_usec = 0; FD_ZERO(&set); FD_SET(sockfd, &set); maxfd = sockfd+1; select(maxfd, &set, NULL, NULL, &tv); if(FD_ISSET(sockfd, &set)){ // let's choose some smaller value than 1500 bytes. bzero(buf, ETHFR_MAXDATA_LEN); printf("Waiting for request..."); int length = recvfrom(sockfd, buf, ETHFR_MAXDATA_LEN, 0, NULL, NULL); if (length == -1) { printFailed(); free(buf); return length; } printOK(); debug("Got packed payload, length = %d", length); unpackPayload(buf, &ph); strcpy(srcIpAddr, printIPHuman(ph.srcIp)); *srcPort = ph.srcPort; strcpy(msg, ph.msg); free(buf); return length; } free(buf); debug("Nothing read. Timeout."); return -1;//timeout } }
int sendRtMsg(int sd){ int status, datalen, *ip_flags; char *target, *src_ip, *dst_ip; struct ip iphdr; uint8_t *data, *packet; struct icmp icmphdr; struct addrinfo hints, *res; struct sockaddr_in *ipv4, sin; void *tmp; // Allocate memory for various arrays. data = allocate_ustrmem (IP_MAXPACKET); packet = allocate_ustrmem (IP_MAXPACKET); target = allocate_strmem (40); src_ip = allocate_strmem (INET_ADDRSTRLEN); dst_ip = allocate_strmem (INET_ADDRSTRLEN); ip_flags = allocate_intmem (4); strcpy(src_ip, inet_ntoa(ip_list[0])); strcpy(target, inet_ntoa(ip_list[1])); debug("Source IP %s, targe IP %s", src_ip, target); // Fill out hints for getaddrinfo(). memset (&hints, 0, sizeof (struct addrinfo)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = hints.ai_flags | AI_CANONNAME; // Resolve target using getaddrinfo(). if ((status = getaddrinfo (target, NULL, &hints, &res)) != 0) { fprintf (stderr, "getaddrinfo() failed: %s\n", gai_strerror (status)); exit (EXIT_FAILURE); } ipv4 = (struct sockaddr_in *) res->ai_addr; tmp = &(ipv4->sin_addr); if (inet_ntop (AF_INET, tmp, dst_ip, INET_ADDRSTRLEN) == NULL) { status = errno; fprintf (stderr, "inet_ntop() failed.\nError message: %s", strerror (status)); exit (EXIT_FAILURE); } freeaddrinfo (res); datalen = sizeof(struct tourdata) + (VMcount * 4); struct tourdata td; td.index = htonl(0); td.nodes_in_tour = htonl(VMcount); td.mult_ip = inet_addr(MULTICAST_IP); td.mult_port = htons(MULTICAST_PORT); memcpy(data, &td, sizeof(struct tourdata)); subscribeToMulticast(&td); void * ptr = data; ptr = ptr + sizeof(struct tourdata); int i; //Add the IPs of the VMs to visit for(i = 1; i <=VMcount; i++){ memcpy(ptr, &ip_list[i], 4); struct in_addr * temp2 = (struct in_addr *) ptr; ptr = ptr + 4; } // IPv4 header // IPv4 header length (4 bits): Number of 32-bit words in header = 5 iphdr.ip_hl = IP4_HDRLEN / sizeof (uint32_t); iphdr.ip_v = 4;// Internet Protocol version (4 bits): IPv4 iphdr.ip_tos = 0;// Type of service (8 bits) iphdr.ip_len = htons (IP4_HDRLEN + ICMP_HDRLEN + datalen);// Total length of datagram (16 bits): IP header + UDP header + datalen iphdr.ip_id = htons (MY_IP_ID);// ID sequence number (16 bits): unused, since single datagram // Flags, and Fragmentation offset (3, 13 bits): 0 since single datagram ip_flags[0] = 0; ip_flags[1] = 0;// Do not fragment flag (1 bit) ip_flags[2] = 0;// More fragments following flag (1 bit) ip_flags[3] = 0;// Fragmentation offset (13 bits) iphdr.ip_off = htons ((ip_flags[0] << 15) + (ip_flags[1] << 14) + (ip_flags[2] << 13) + ip_flags[3]); iphdr.ip_ttl = 255;// Time-to-Live (8 bits): default to maximum value iphdr.ip_p = RT_PROTO;// Transport layer protocol (8 bits): 1 for ICMP // Source IPv4 address (32 bits) if ((status = inet_pton (AF_INET, src_ip, &(iphdr.ip_src))) != 1) { fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status)); exit (EXIT_FAILURE); } // Destination IPv4 address (32 bits) if ((status = inet_pton (AF_INET, dst_ip, &(iphdr.ip_dst))) != 1) { fprintf (stderr, "inet_pton() failed.\nError message: %s", strerror (status)); exit (EXIT_FAILURE); } // IPv4 header checksum (16 bits): set to 0 when calculating checksum iphdr.ip_sum = 0; iphdr.ip_sum = checksum ((uint16_t *) &iphdr, IP4_HDRLEN); // ICMP header icmphdr.icmp_type = 0;// Message Type (8 bits): echo request icmphdr.icmp_code = 0;// Message Code (8 bits): echo request icmphdr.icmp_id = htons (RT_ICMPID);// Identifier (16 bits): usually pid of sending process - pick a number icmphdr.icmp_seq = htons (0);// Sequence Number (16 bits): starts at 0 icmphdr.icmp_cksum = 0; icmphdr.icmp_cksum = icmp4_checksum(icmphdr, data, datalen); // Prepare packet. // First part is an IPv4 header. memcpy (packet, &iphdr, IP4_HDRLEN); // Next part of packet is upper layer protocol header. memcpy ((packet + IP4_HDRLEN), &icmphdr, ICMP_HDRLEN); // Finally, add the ICMP data. memcpy (packet + IP4_HDRLEN + ICMP_HDRLEN, data, datalen); // Calculate ICMP header checksum //icmphdr.icmp_cksum = 0;//checksum ((uint16_t *) (packet + IP4_HDRLEN), ICMP_HDRLEN + datalen); //memcpy ((packet + IP4_HDRLEN), &icmphdr, ICMP_HDRLEN); // The kernel is going to prepare layer 2 information (ethernet frame header) for us. // For that, we need to specify a destination for the kernel in order for it // to decide where to send the raw datagram. We fill in a struct in_addr with // the desired destination IP address, and pass this structure to the sendto() function. memset (&sin, 0, sizeof (struct sockaddr_in)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = iphdr.ip_dst.s_addr; if (sendto (sd, packet, IP4_HDRLEN + ICMP_HDRLEN + datalen, 0, (struct sockaddr *) &sin, sizeof (struct sockaddr)) < 0) { perror ("sendto() failed "); exit (EXIT_FAILURE); } printOK(); // Free allocated memory. free (data); free (packet); free (target); free (src_ip); free (dst_ip); free (ip_flags); return 0; }
void ModemBase::processData() { digitalWrite(RTS, LOW); //if(digitalRead(DCE_CTS) == HIGH) Serial.write("::DCE_CTS is high::"); //if(digitalRead(DCE_CTS) == LOW) Serial.write("::DCE_CTS is low::"); while(_serial->available()) { if(_isCommandMode) { char inbound = toupper(_serial->read()); if(_echoOn) // && inbound != _S2_escapeCharacter { _serial->write(inbound); } if (inbound == S2_escapeCharacter) { _escapeCount++; } else { _escapeCount = 0; } if(_escapeCount == 3) { _escapeCount = 0; // TODO, guard time! printOK(); } if (inbound == S5_bsCharacter) { if(strlen(_commandBuffer) > 0) { _commandBuffer[strlen(_commandBuffer) - 1] = '\0'; } } else if (inbound != '\r' && inbound != '\n' && inbound != S2_escapeCharacter) { _commandBuffer[strlen(_commandBuffer)] = inbound; if (_commandBuffer[0] == 'A' && _commandBuffer[1] == '/') { strcpy(_commandBuffer, _lastCommandBuffer); processCommandBuffer(); resetCommandBuffer(); // To prevent A matching with A/ again } } else if(_commandBuffer[0] == 'A' && _commandBuffer[1] == 'T') { processCommandBuffer(); } else { resetCommandBuffer(); } } else { if(_isConnected) { char inbound = _serial->read(); if(_echoOn) _serial->write(inbound); if (inbound == S2_escapeCharacter) // TODO - refactor to get rid of duplication above { _escapeCount++; } else { _escapeCount = 0; } if(_escapeCount == 3) { _escapeCount = 0; _isCommandMode = true; // TODO, guard time! printOK(); } if (!_isCommandMode) { int result = _wifly->write(inbound); } } } } //digitalWrite(DCE_RTS, LOW); }
void ModemBase::processCommandBuffer() { for (int i=0; i < strlen(_commandBuffer); ++i) { _commandBuffer[i] = toupper(_commandBuffer[i]); } if (strcmp(_commandBuffer, ("AT/")) == 0) { strcpy(_commandBuffer, _lastCommandBuffer); } if (strcmp(_commandBuffer, ("ATZ")) == 0) { loadDefaults(); printOK(); } else if (strcmp(_commandBuffer, ("ATI")) == 0) { ShowInfo(); printOK(); } else if (strcmp(_commandBuffer, ("AT&F")) == 0) { if(strcmp(_lastCommandBuffer, ("AT&F")) == 0) { loadDefaults(); printOK(); } else { _serial->println(F("send command again to verify.")); } } else if(strcmp(_commandBuffer, ("ATA")) == 0) { answer(); } else if (strcmp(_commandBuffer, ("ATD")) == 0 || strcmp(_commandBuffer, ("ATO")) == 0) { if (_isConnected) { _isCommandMode = false; } else { printError(); } } else if( strncmp(_commandBuffer, ("ATDT "), 5) == 0 || strncmp(_commandBuffer, ("ATDP "), 5) == 0 || strncmp(_commandBuffer, ("ATD "), 4) == 0 ) { onDialout(strstr(_commandBuffer, " ") + 1); resetCommandBuffer(); // This avoids port# string fragments on subsequent calls } else if (strncmp(_commandBuffer, ("ATDT"), 4) == 0) { onDialout(strstr(_commandBuffer, "ATDT") + 4); resetCommandBuffer(); // This avoids port# string fragments on subsequent calls } else if ((strcmp(_commandBuffer, ("ATH0")) == 0 || strcmp(_commandBuffer, ("ATH")) == 0)) { disconnect(); } else if(strncmp(_commandBuffer, ("AT"), 2) == 0) { if(strstr(_commandBuffer, ("E0")) != NULL) { _echoOn = false; } if(strstr(_commandBuffer, ("E1")) != NULL) { _echoOn = true; } if(strstr(_commandBuffer, ("Q0")) != NULL) { _verboseResponses = false; _quietMode = false; } if(strstr(_commandBuffer, ("Q1")) != NULL) { _quietMode = true; } if(strstr(_commandBuffer, ("V0")) != NULL) { _verboseResponses = false; } if(strstr(_commandBuffer, ("V1")) != NULL) { _verboseResponses = true; } if(strstr(_commandBuffer, ("X0")) != NULL) { // TODO } if(strstr(_commandBuffer, ("X1")) != NULL) { // TODO } char *currentS; char temp[100]; int offset = 0; if((currentS = strstr(_commandBuffer, ("S0="))) != NULL) { offset = 3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S0_autoAnswer = atoi(temp); } /* if((currentS = strstr(_commandBuffer, ("S1="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S1_ringCounter = atoi(temp); #if DEBUG == 1 lggr.print(F("S1=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S2="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S2_escapeCharacter = atoi(temp); #if DEBUG == 1 lggr.print(F("S2=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S3="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S3_crCharacter = atoi(temp); #if DEBUG == 1 lggr.print(F("S3=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S4="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S4_lfCharacter = atoi(temp); #if DEBUG == 1 lggr.print(F("S4=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S5="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S5_bsCharacter = atoi(temp); #if DEBUG == 1 lggr.print(F("S5=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S6="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S6_waitBlindDial = atoi(temp); #if DEBUG == 1 lggr.print(F("S6=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S7="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S7_waitForCarrier = atoi(temp); #if DEBUG == 1 lggr.print(("S7=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S8="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S8_pauseForComma = atoi(temp); #if DEBUG == 1 lggr.print(F("S8=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S9="))) != NULL) { offset =3; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 3, offset - 3); _S9_cdResponseTime = atoi(temp); #if DEBUG == 1 lggr.print(F("S9=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S10="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S10_delayHangup = atoi(temp); #if DEBUG == 1 lggr.print(F("S10=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S11="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S11_dtmf = atoi(temp); #if DEBUG == 1 lggr.print(F("S11=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S12="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S12_escGuardTime = atoi(temp); #if DEBUG == 1 lggr.print(F("S12=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S18="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S18_testTimer = atoi(temp); #if DEBUG == 1 lggr.print(F("S18=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S25="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S25_delayDTR = atoi(temp); #if DEBUG == 1 lggr.print(F("S25=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S26="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S26_delayRTS2CTS = atoi(temp); #if DEBUG == 1 lggr.print(F("S26=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S30="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S30_inactivityTimer = atoi(temp); #if DEBUG == 1 lggr.print(F("S30=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S37="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S37_lineSpeed = atoi(temp); #if DEBUG == 1 lggr.print(F("S37=")); lggr.println(temp); #endif EEPROM.write(S37_ADDRESS, _S37_lineSpeed); setLineSpeed(); #if DEBUG == 1 lggr.print(F("Set Baud Rate: ")); lggr.println(_baudRate); #endif } if((currentS = strstr(_commandBuffer, ("S38="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _S38_delayForced = atoi(temp); #if DEBUG == 1 lggr.print(F("S38=")); lggr.println(temp); #endif } if((currentS = strstr(_commandBuffer, ("S90="))) != NULL) { offset =4; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 4, offset - 4); _isDcdInverted = atoi(temp); #if DEBUG == 1 lggr.print(F("S90=")); lggr.println(temp); #endif print(F("SAVED S90=")); println(temp); EEPROM.write(S90_ADDRESS, atoi(temp)); } if((currentS = strstr(_commandBuffer, ("S200="))) != NULL) { offset =5; while(currentS[offset] != '\0' && isDigit(currentS[offset])) { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 5, offset - 5); _baudRate = atol(temp); setDefaultBaud(_baudRate); #if DEBUG == 1 lggr.print(F("S200=")); lggr.println(temp); #endif // print(F("SAVED S200=")); println(temp); // EEPROM.write(S90_ADDRESS, atoi(temp)); } for(int i=MAC_1 + 300; i<=USE_DHCP + 300; ++i) { char reg[6]; sprintf(reg, "S%u=", i); if((currentS = strstr(_commandBuffer, reg)) != NULL) { int value = atoi(currentS + 5); EEPROM.write(i - 300, value); print(F("SAVED S")); print(i); print("="); println(value); } } for(int i=1; i<10; ++i) { char reg[6]; sprintf(reg, ("S10%u="), i); if((currentS = strstr(_commandBuffer, reg)) != NULL) { int offset = 5; while(currentS[offset] != '\0') { offset++; } memset(temp, 0, 100); strncpy(temp, currentS + 5, offset - 5); #if DEBUG == 1 lggr.print(reg); lggr.println(temp); #endif print(F("SAVED ")); print(reg); print('='); println(temp); writeAddressBook(ADDRESS_BOOK_START + (ADDRESS_BOOK_LENGTH * i), temp); } } */ printOK(); } else { printError(); } strcpy(_lastCommandBuffer, _commandBuffer); resetCommandBuffer(); }
int sendRtMsgIntermediate(int sd, void * buf, ssize_t len){ int status, *ip_flags; struct ip iphdr; uint8_t *data, *packet; struct icmp icmphdr; struct sockaddr_in *ipv4, sin; void *tmp; // Allocate memory for various arrays. data = allocate_ustrmem (IP_MAXPACKET); packet = allocate_ustrmem (IP_MAXPACKET); ip_flags = allocate_intmem (4); //Unpack the tourdata struct tourdata * unpack = (struct tourdata *)buf; subscribeToMulticast(unpack); //since we work and change index unpack->index = ntohl(unpack->index); printf("Index:%d, nodes in tour:%d\n", unpack->index, ntohl(unpack->nodes_in_tour)); if((unpack->index + 1) == ntohl(unpack->nodes_in_tour)){ //Tour has ended. Send multicast here to everyone and return actually //you dont send anyting from here endOfTour = 1; free (data); free (packet); free (ip_flags); return 0; } int itemsArrSizeBytes = ntohl(unpack->nodes_in_tour) * sizeof(struct in_addr); int datalen = sizeof(struct tourdata) + itemsArrSizeBytes; // IPv4 header // IPv4 header length (4 bits): Number of 32-bit words in header = 5 iphdr.ip_hl = IP4_HDRLEN / sizeof (uint32_t); iphdr.ip_v = 4;// Internet Protocol version (4 bits): IPv4 iphdr.ip_tos = 0;// Type of service (8 bits) iphdr.ip_len = htons (IP4_HDRLEN + ICMP_HDRLEN + datalen);// Total length of datagram (16 bits): IP header + UDP header + datalen iphdr.ip_id = htons (MY_IP_ID);// ID sequence number (16 bits): unused, since single datagram debug("hey"); // Flags, and Fragmentation offset (3, 13 bits): 0 since single datagram ip_flags[0] = 0; ip_flags[1] = 0;// Do not fragment flag (1 bit) ip_flags[2] = 0;// More fragments following flag (1 bit) ip_flags[3] = 0;// Fragmentation offset (13 bits) iphdr.ip_off = htons ((ip_flags[0] << 15) + (ip_flags[1] << 14) + (ip_flags[2] << 13) + ip_flags[3]); iphdr.ip_ttl = 255;// Time-to-Live (8 bits): default to maximum value iphdr.ip_p = RT_PROTO;// Transport layer protocol (8 bits): 1 for ICMP debug("hey"); void * ptr = buf + sizeof(struct tourdata); ptr = ptr + (sizeof(struct in_addr) * unpack->index); //current node-item iphdr.ip_src = *(struct in_addr *)ptr; unpack->index++; ptr = ptr + sizeof(struct in_addr); //advance the pointer to the next in_addr_t iphdr.ip_dst = *(struct in_addr *)ptr; debug("hey. Src IP :%s\n", printIPHuman(iphdr.ip_src.s_addr)); debug("hey. Dest IP :%s\n", printIPHuman(iphdr.ip_dst.s_addr)); // IPv4 header checksum (16 bits): set to 0 when calculating checksum iphdr.ip_sum = 0; iphdr.ip_sum = checksum ((uint16_t *) &iphdr, IP4_HDRLEN); // ICMP header icmphdr.icmp_type = 0;// Message Type (8 bits): echo request icmphdr.icmp_code = 0;// Message Code (8 bits): echo request icmphdr.icmp_id = htons (RT_ICMPID);// Identifier (16 bits): usually pid of sending process - pick a number icmphdr.icmp_seq = htons (0);// Sequence Number (16 bits): starts at 0 icmphdr.icmp_cksum = 0; icmphdr.icmp_cksum = icmp4_checksum(icmphdr, data, datalen); // Copy tour packet to the new ICMP data payload unpack->index = htonl(unpack->index); memcpy(data, unpack, sizeof(struct tourdata)); memcpy((void*)(data + sizeof(struct tourdata)), (void*)(buf + sizeof(struct tourdata)), itemsArrSizeBytes); debug("hey itemarrsizebytes %d", itemsArrSizeBytes); // Prepare packet. // First part is an IPv4 header. memcpy (packet, &iphdr, IP4_HDRLEN); // Next part of packet is upper layer protocol header. debug("hey"); memcpy ((packet + IP4_HDRLEN), &icmphdr, ICMP_HDRLEN); // Finally, add the ICMP data. debug("hey"); memcpy (packet + IP4_HDRLEN + ICMP_HDRLEN, data, datalen); debug("hey, datalen=%d", datalen); memset (&sin, 0, sizeof (struct sockaddr_in)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = iphdr.ip_dst.s_addr; debug("hey. Dest IP :%s\n", printIPHuman(ntohl(iphdr.ip_dst.s_addr))); printf("Gonna send..."); // Send packet. if (sendto (sd, packet, IP4_HDRLEN + ICMP_HDRLEN + datalen, 0, (struct sockaddr *) &sin, sizeof (struct sockaddr)) < 0) { printFailed(); return 1; } printOK(); // Free allocated memory. free (data); free (packet); free (ip_flags); return 0; }