Example #1
0
int main (void) {
    MBED_HOSTTEST_TIMEOUT(20);
    MBED_HOSTTEST_SELECT(udpecho_server_auto);
    MBED_HOSTTEST_DESCRIPTION(UDP echo server);
    MBED_HOSTTEST_START("NET_5");

    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("MBED: Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT);

    UDPSocket server;
    server.bind(ECHO_SERVER_PORT);

    Endpoint client;
    char buffer[BUFFER_SIZE] = {0};
    printf("MBED: Waiting for packet...\r\n");
    while (true) {
        int n = server.receiveFrom(client, buffer, sizeof(buffer));
        if (n > 0) {
            //printf("Received packet from: %s\n", client.get_address());
            const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n;
            buffer[buffer_string_end_index] = '\0';
            //printf("Server received: %s\n", buffer);
            server.sendTo(client, buffer, n);
        }
    }
}
Example #2
0
void udp_server_task(void const *argument)
{
    DigitalOut indicator(LED1);
    UDPSocket server;

    server.bind(ECHO_SERVER_PORT);
    // printf("[udp_server_task] Start\r\n");

    Endpoint client;
    char buffer[BUFFER_SIZE] = { 0 };
    while (true) {
        //printf("[udp_server_task] Wait for packet...\r\n");
        int n = server.receiveFrom(client, buffer, sizeof(buffer));
        if (n > 0) {
            //printf("[udp_server_task] Received packet from: %s\r\n", client.get_address());
            const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n;
            buffer[buffer_string_end_index] = '\0';
            //printf("[udp_server_task] Server received: %s\r\n", buffer);
            if (host_port == 0) {
                strcpy(host_address, client.get_address());
                host_port = ECHO_SERVER_PORT + 1;
                //printf("[udp_server_task] Set host address and port: %s:%d\r\n", host_address, host_port);
            }
            // Dispatch data to client for sending to test HOST
            cli_serv_mutex.lock(); // LOCK
            // Push to datagram queue
            datagram_queue.push_front(std::string(buffer));
            max_queue_len = datagram_queue.size() > max_queue_len ? datagram_queue.size() : max_queue_len;
            received_packets++;
            cli_serv_mutex.unlock(); // LOCK
            indicator = !indicator;
        }
    }
}
Example #3
0
int main (void) {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());
    
    UDPSocket server;
    server.bind(ECHO_SERVER_PORT);
    
    Endpoint client;
    char buffer[256];
    while (true) {
        printf("\nWait for packet...\n");
        int n = server.receiveFrom(client, buffer, sizeof(buffer));
        
        printf("Received packet from: %s\n", client.get_address());
        server.sendTo(client, buffer, n);
    }
}
Example #4
0
File: main.cpp Project: AsamQi/mbed
int main() {
    bool result = false;
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("UDP client IP Address is %s\n", eth.getIPAddress());

    UDPSocket sock;
    sock.init();

    Endpoint nist;
    nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT);

    char out_buffer[] = "plop"; // Does not matter
    sock.sendTo(nist, out_buffer, sizeof(out_buffer));

    union {
        char in_buffer_tab[4];
        unsigned int in_buffer_uint;
    };

    const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab));
    if (n > 0) {
        result = true;
        const unsigned int timeRes = ntohl(in_buffer_uint);
        const float years = timeRes / 60.0 / 60.0 / 24.0 / 365;
        printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port());
        printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]");
        printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > YEARS_TO_PASS ? "[OK]" : "[FAIL]");

        if (years < YEARS_TO_PASS) {
            result = false;
        }
    }
    sock.close();
    eth.disconnect();
    notify_completion(result);
    return 0;
}
Example #5
0
int main (void) {
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT);
    
    UDPSocket server;
    server.bind(ECHO_SERVER_PORT);
    
    Endpoint client;
    char buffer[BUFFER_SIZE] = {0};
    while (true) {
        printf("Wait for packet...\n");
        int n = server.receiveFrom(client, buffer, sizeof(buffer));
        if (n > 0) {
            printf("Received packet from: %s\n", client.get_address());
            const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n;
            buffer[buffer_string_end_index] = '\0';
            printf("Server received: %s\n", buffer);
            server.sendTo(client, buffer, n);
        }
    }
}
Example #6
0
int main()
{
    int result;

    lwm2m_object_t * objArray[4];

    printf("Start\n");
    ethSetup();

    printf("Initializing tinydtls\n");

    // fake loading of PSK..
    psk_id_length = strlen(PSK_DEFAULT_IDENTITY);
    psk_key_length = strlen(PSK_DEFAULT_KEY);
    memcpy(psk_id, PSK_DEFAULT_IDENTITY, psk_id_length);
    memcpy(psk_key, PSK_DEFAULT_KEY, psk_key_length);
    printf("Init\n");

    dtls_init();
    printf("New context\n");
    dtls_context = dtls_new_context(&lwm2mH);

    if (dtls_context == NULL) {
        printf("error creating the dtls context\n");
    }
    printf("Setting handlers\n");

    dtls_set_handler(dtls_context, &cb);

    if (!dtls_context) {
        printf("can't create dtls_context\n");
        exit(-1);
    }

    printf("Initialazing Wakaama\n");

    // create objects
    objArray[0] = get_security_object(123, "coaps://5.39.83.206:5684", false);
    securityObjP = objArray[0];

    objArray[1] = get_server_object(123, "U", 20, false);
    serverObject = objArray[1];
    objArray[2] = get_object_device();
    objArray[3] = get_object_firmware();

    /*
     * The liblwm2m library is now initialized with the functions that will be in
     * charge of communication
     */
    lwm2mH = lwm2m_init(prv_connect_server, prv_buffer_send, NULL);
    if (NULL == lwm2mH)
    {
        fprintf(stderr, "lwm2m_init() failed\r\n");
        return -1;
    }

    // configure the liblwm2m lib

    result = lwm2m_configure(lwm2mH, "julien", NULL, 4, objArray);

    if (result != 0)
    {
        printf("lwm2m_configure() failed: 0x%X\n", result);
        return -1;
    }

    // start

    result = lwm2m_start(lwm2mH);
    if (result != 0)
    {
        printf("lwm2m_start() failed: 0x%X\n", result);
        return -1;
    }

    // main loop

    while (true) {
        char buffer[1024];
        Endpoint server;

        printf("loop...\n");
        struct timeval timeout;
        timeout.tv_sec = 10;
        timeout.tv_usec = 0;

        result = lwm2m_step(lwm2mH, &timeout);
        if (result != 0)
        {
            printf("lwm2m_step error %d\n", result);
        }
        int n = udp.receiveFrom(server, buffer, sizeof(buffer));
        printf("Received packet from: %s of size %d\n", server.get_address(), n);
        if (n>0) {
            // TODO: find connection
            connection_t * connP = connList;
            while(connP != NULL) {
                if (strcmp(connP->host, server.get_address()) == 0)
                {

                    printf("found connection\n");
                    // is it a secure connection?
                    if (connP->dtlsSession != NULL) {
						printf("dtls session\n");
						result = dtls_handle_message(dtls_context, connP->dtlsSession, buffer, n);
						printf("dtls handle message %d\n",result);
					} else {
						printf("nosec session\n");
						lwm2m_handle_packet(lwm2mH, (uint8_t*)buffer, n, (void*)connP);
					}
                    break;
                }
            }

            if (connP == NULL) printf("no connection\n");
        }
    }
}
void main(void)
{
    //***************** BEGINNING OF ETHERNET SETUP [DONT EDIT] *****************//
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());

    UDPSocket server;
    server.bind(ECHO_SERVER_PORT);

    Endpoint client;
    char buffer[256];

    //***************** END OF ETHERNET SETUP **********************************//

    //***************** BEGINNING OF WIRELESS SETUP [DONT EDIT] *****************//

    uint8_t channel = 6;
    //Set the Channel. 0 is default, 15 is max
    mrf.SetChannel(channel);
    //Start the timer
    timer.start();

    //***************** END OF WIRELESS SETUP **********************************//
    raiseAllPins();
    dropAllPins();


    speedChecker.attach(&speedLogic,0.005); //sets ticker for interrupts
    dcOUT=0; // turns DC motor off initially

    int cycles = 0;
    int flush = 0;
    int startChar =0;
    while(1) {
        dcPWM.write(dutyCycle);
        cycles = cycles-1;

        if(needsInput==true) {
            //What MBED is receiving from client?
            if(flush) {
                int lengthBuffer = strlen(buffer);
                for(int i = 0; i <lengthBuffer; i++) {
                    buffer[i]='\0';
                }
            }
            printf("\nWait for packet...\n\r");
            lightShow.drawChar('@'); //this should draw the character on the screen
            int n = server.receiveFrom(client, buffer, sizeof(buffer));
            printf("\nReceived packet...\n\r");
            printf("\nReceived integer n...%i\n\r",n);
            buffer[n]='\0';
            printf("\nyour word is: %s\n\r",buffer);
            server.sendTo(client, buffer, n); //echo protocol
            needsInput=false;
            cycles=n;
            /*revive code*/
            sendDelay = slowSpeed; //pins
            dutyCycle = slowMotor; //motor
            dcOUT = 1; //turn on motor
            off=false;
            slow = true;
            startChar=0;

        }
        if(cycles<=0) {
            needsInput= true;
            slow = false;
            off=true;
            dcOUT = 0; //turn motor off
            sendDelay = slowSpeed; //pins
            dutyCycle = slowMotor; //for whenever it turns on after you turned it off using REDUCE
        }

        if(!off) {
            char character = getNextCharacter(startChar, buffer);
            startChar=1;
            printf("\nchar: %c\n\r",character);
            int pinBinary = braille.matchCharacter(character);

            if(braille.isNumber(character)) {
                handleNumberCase(character);
            }
            led1=1;
            lightShow.drawChar(character); //this should draw the character on the screen
            led1=0;
            wait_ms(sendDelay);

            sendNumber(pinBinary);
            printf("Pinbinary: %i\n\r",pinBinary);

            //***** ACKNOWLEDGE CODE ******//

            int recLength = rf_receive(rxbuffer,128);
            while(recLength<=0) {
                led2=1;
                recLength = rf_receive(rxbuffer,128);
            }
            led2=0;
            //***** END ACKNOWLEDGE CODE ******//

        }//end if motor stopped code
        dropAllPins();
    }//end while loop
}//end all code