THREAD(udpTrapReceiveThread, arg) { //NutThreadSetPriority(32); NH * nh = (NH*)arg; uint8_t data[256]; uint32_t addr; uint16_t port; int i; for(;;) { i = NutUdpReceiveFrom(nh->udpTrapSock, &addr, &port, data, 256, 0); //printf("Some trap traffic...\n"); data[i] = '\0'; if (i > 0) { //printf("a trap received...\n"); if(nh->opponent == addr) { snmpPacket* packetIn = snmp_packetalloc(); //input snmp_parse(packetIn, data, i); uint8_t* ptr = (uint8_t*)packetIn->value; strcpy(nh->trapMessage, ptr); // printf("Viesti: \"%s\"\n", ptr); nh->opponent = 0; snmp_free(packetIn); } }else if( i== 0) printf("trap thread running\n"); else printf("trap thread running\n"); } }
THREAD(DiscoveryResponder, arg) { UDPSOCKET *sock; DISCOVERY_TELE *dist; u_long raddr; u_short rport; int len; /* Insist on allocating a datagram buffer. */ while ((dist = malloc(sizeof(DISCOVERY_TELE))) == NULL) { NutSleep(1000); } /* Insist on creating a socket. */ while ((sock = NutUdpCreateSocket(disopt.disopt_port)) == NULL) { NutSleep(1000); } /* Nut/Net doesn't provide UDP datagram buffering by default. */ { u_short max_ms = sizeof(DISCOVERY_TELE) * 3; NutUdpSetSockOpt(sock, SO_RCVBUF, &max_ms, sizeof(max_ms)); } /* Create a pseudo random telegram identifier. */ memcpy(&xid, &confnet.cdn_mac[2], sizeof(xid)); xid += NutGetTickCount(); /* Optionally send initial announce datagram. */ if (disopt.disopt_flags & DISF_INITAL_ANN) { /* Variable sleep avoids broadcast storms in large installations. */ NutSleep(500 + (xid & 0x7FF)); if ((len = NutDiscoveryAnnTele(dist)) > 0) { NutUdpSendTo(sock, INADDR_BROADCAST, disopt.disopt_port, dist, len); } } /* Handle discovery telegrams in an endless loop. */ for (;;) { len = NutUdpReceiveFrom(sock, &raddr, &rport, dist, sizeof(DISCOVERY_TELE), 0); /* Updates may be filtered by an IP mask. */ if ((raddr & disopt.disopt_ipmask) == raddr && len > 0 && discovery_callback) { if ((len = discovery_callback(raddr, rport, dist, len)) > 0) { /* Broadcast response if destination is unreachable. */ if ((raddr & confnet.cdn_ip_mask) != (confnet.cdn_ip_addr & confnet.cdn_ip_mask)) { raddr = INADDR_BROADCAST; } NutUdpSendTo(sock, raddr, disopt.disopt_port, dist, len); } } /* Avoid system blocking by datagram storms. */ NutSleep(100); } }
THREAD(UDPReceiver, arg) { uint32_t remote_ip; uint16_t remote_port; UDPSOCKET *socket = (UDPSOCKET*) arg; int rc; int error; while (1) { rc = NutUdpReceiveFrom(socket, &remote_ip, &remote_port, rcv_buffer, UDP_BUFF_SIZE, 2000); if (rc == 0) { printf("<-- Timeout (2 sec.) on UDP receive\r\n"); } else if (rc < 0) { error = NutUdpError(socket, &remote_ip, &remote_port); print_udp_icmp_error(remote_ip, remote_port, error); } else { printf("<-- Received packet: \"%s\"\r\n", rcv_buffer); } } }
THREAD(udpReceiveThread, arg) { NH * nh = (NH*)arg; uint8_t data[256]; uint32_t addr; uint16_t port; int i; for(;;) { i = NutUdpReceiveFrom(nh->udpSock, &addr, &port, data, 256, 0); data[i] = '\0'; if (i > 0) { //printf("Udp connection\n"); //printf("%s:%d \n", inet_ntoa(addr), port); //fflush(stdout); //bool doCleanPacketIn = true; snmpPacket* packetIn = snmp_packetalloc(); snmpPacket* packetOut = snmp_packetalloc(); snmp_parse(packetIn, data, i); packetOut->command = SNMP_GET_RESP; packetOut->requestID = packetIn->requestID; packetOut->objectID = packetIn->objectID; long value; // --- vastataan ready to play -viestiin if(packetIn->objectID == 1 && packetIn->command == SNMP_GET_REQ) //READY-TO-PLAY -message { if(nh->readyToPlay == true) value = 1; else value = 0; packetOut->valueType = SNMP_INT; packetOut->valueLength = 1; packetOut->value = &value; packetOut->error = 0x00; snmp_construct(packetOut); NutUdpSendTo(nh->udpSock, addr, port, packetOut->packet, packetOut->length); } // --- odotetaan vastausta ready to play viestiin else if(nh->getOpponentList && packetIn->objectID == 1 && packetIn->command == SNMP_GET_RESP) { uint8_t tmp; tmp = *((uint8_t*)(packetIn->value)); if(tmp == 1) //fill the opponent list, if broadcast is sent { nh->opponentList[nh->numOpponents] = addr; nh->numOpponents++; } } // --- haaste vastaanotettu else if (nh->opponent == 0 && packetIn->objectID == 2 && packetIn->command == SNMP_SET_REQ) { nh->openRequest = true; nh->opponent = addr; nh->requestID = packetIn->requestID; } //haaste vastaanotettu, mutta meillä oli jo haaste päällä else if ( (nh->openRequest || addr != nh->opponent) && packetIn->objectID == 2 && packetIn->command == SNMP_SET_REQ) NH_sendSnmpError(nh, addr, SNMP_PORT, SNMP_GET_RESP, 2, packetOut->requestID); // --- odotetaan vastausta haasteeseen else if( packetIn->objectID == 2 && nh->challengeSend == true && nh->opponent == addr && packetIn->command == SNMP_GET_RESP ) { nh->challengeSend = false; if( packetIn->error != 0x00) nh->opponent = false; else nh->challengeAccepted = true; } // --- odotetaan vastustajan START-TURN else if(packetIn->objectID == 2 && nh->opponent == addr && packetIn->command == SNMP_SET_REQ ) { nh->enemyStartedTurn = true; //printf("NW: enemy started turn\n"); } // --- odotetaan vastustajalta OK-viestiä else if(nh->opponent == addr && packetIn->command == SNMP_GET_RESP && packetIn->objectID == nh->currentOID && packetIn->valueType == SNMP_INT) { if(packetIn->error == 0x00) { nh->enemyOK = true; //printf("NW: Received enemyOK!\n"); } } // --- odotetaan salvoja else if( packetIn->objectID == 3 && nh->opponent == addr && packetIn->command == SNMP_SET_REQ ) { nh->enemySalvoReceived = true; //printf("NW: SALVO received (%d):\n", nh->enemySalvoReceived); // debug uint8_t* ptr = (uint8_t*)packetIn->value; for(i = 0; i < 5; i++) { nh->lastEnemySalvo[i].x = ptr[i*2]; nh->lastEnemySalvo[i].y = ptr[i*2+1]; //printf("%d - %d\n", nh->lastEnemySalvo[i].x, nh->lastEnemySalvo[i].y); } // OK vastaus NH_sendBooleanResponse(nh, 3, true); } // --- Odotetaan salvon tuloksia else if( packetIn->objectID == 4 && nh->opponent == addr && packetIn->command == SNMP_GET_RESP) { //printf("NW: SALVO results received: \n"); // debug uint8_t* ptr = (uint8_t*)packetIn->value; for (i = 0; i < 5; i++) nh->salvoResults[i] = ptr[i]; //debug //for (i = 0; i < 5; i++) //printf("%d: %d\n", i, nh->salvoResults[i]); nh->salvoResultsReceived = true; } // --- odotetaan salvon tuloksien pyyntöä else if(packetIn->objectID == 4 && packetIn->command == SNMP_GET_REQ && nh->opponent == addr) { nh->resultsQueryReceived = true; //printf("NW: enemy asked for salvo results: %d\n", nh->resultsQueryReceived); } // --- chattiviesti: else if(packetIn->objectID == 5 && packetIn->command == SNMP_SET_REQ && packetIn->valueType == SNMP_OCT) { /* uint8_t* ptr = (uint8_t*)packetIn->value; //lisätään viestin alkuun lähettähän ip sprintf(nh->chatMsgs[nh->emptyMsg], inet_ntoa(addr)); strcat(nh->chatMsgs[nh->emptyMsg], ":\n"); strncat(nh->chatMsgs[nh->emptyMsg], message, MAX_MSG_LENGTH - 16); //lisätään viesti listaan sprintf(chatMsgs[nh->emptyMsg], ptr); nh->manageMessages(); */ } else if(packetIn->command == SNMP_GET_REQ) //debug { printf("NW: weird SNMP_GET_REQ received, OID: %d, opponent: %s", packetIn->objectID, inet_ntoa(nh->opponent)); } snmp_free(packetIn); snmp_free(packetOut); } else if (i == 0) { printf ("Udp timeout\n"); } else { printf ("Udp error\n"); } } }