void app_start(int, char**) { // setup the EthernetInterface eth.init(); // DHCP eth.connect(); // initialize the ipv4 tcp/ip stack lwipv4_socket_init(); // setup the M2MInterface srand(time(NULL)); uint16_t port = rand() % 65535 + 12345; srv = M2MInterfaceFactory::create_interface(observer,endpoint,type, lifetime,port,domain,M2MInterface::UDP,M2MInterface::LwIP_IPv4,context_address); // setup the Security object sec = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer); sec->set_resource_value(M2MSecurity::M2MServerUri,address); sec->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); // setup the Device object dev = M2MInterfaceFactory::create_device(); dev->create_resource(M2MDevice::Manufacturer,"Freescale"); dev->create_resource(M2MDevice::DeviceType,"frdm-k64f"); dev->create_resource(M2MDevice::ModelNumber,"M64FN1MOVLL12"); dev->create_resource(M2MDevice::SerialNumber,"EB1524XXXX"); // setup the sensor objects obj = M2MInterfaceFactory::create_object("loc"); M2MObjectInstance* ins = obj->create_object_instance(); M2MResource* resx = ins->create_dynamic_resource("x","accel",M2MResourceInstance::INTEGER,true); resx->set_operation(M2MBase::GET_PUT_ALLOWED); resx->set_value((const uint8_t*)"0",1); M2MResource* resy = ins->create_dynamic_resource("y","accel",M2MResourceInstance::INTEGER,true); resy->set_operation(M2MBase::GET_PUT_ALLOWED); resy->set_value((const uint8_t*)"0",1); // Assemble the list of objects to register M2MObjectList list; list.push_back(dev); list.push_back(obj); // setup registration event Ticker timer; timer.attach(®,&Registrar::update,20); // enable accelerometer printf("Initializied accelerometer\r\n"); accel.enable(); Ticker sampler; sampler.attach(sample,5); // schedule FunctionPointer1<void, M2MObjectList> fp(®, &Registrar::setup); minar::Scheduler::postCallback(fp.bind(list)); minar::Scheduler::postCallback(sample).period(minar::milliseconds(10000)); minar::Scheduler::start(); }
void tcpClient_main(intptr_t exinf) { EthernetInterface network; TCPSocketConnection socket; /* syslogの設定 */ SVC_PERROR(syslog_msk_log(LOG_UPTO(LOG_INFO), LOG_UPTO(LOG_EMERG))); syslog(LOG_NOTICE, "tcpClient:"); syslog(LOG_NOTICE, "Sample program starts (exinf = %d).", (int_t) exinf); syslog(LOG_NOTICE, "LOG_NOTICE: Network Setting up..."); #if (USE_DHCP == 1) if (network.init() != 0) { //for DHCP Server #else if (network.init(IP_ADDRESS, SUBNET_MASK, DEFAULT_GATEWAY) != 0) { //for Static IP Address (IPAddress, NetMasks, Gateway) #endif syslog(LOG_NOTICE, "Network Initialize Error"); return; } syslog(LOG_NOTICE, "Network was initialized successfully"); while (network.connect(5000) != 0) { syslog(LOG_NOTICE, "LOG_NOTICE: Network Connect Error"); } syslog(LOG_NOTICE, "MAC Address is %s", network.getMACAddress()); syslog(LOG_NOTICE, "IP Address is %s", network.getIPAddress()); syslog(LOG_NOTICE, "NetMask is %s", network.getNetworkMask()); syslog(LOG_NOTICE, "Gateway Address is %s", network.getGateway()); syslog(LOG_NOTICE, "Network Setup OK..."); while (socket.connect(SERVER, HTTP_PORT) < 0) { syslog(LOG_EMERG, "Unable to connect to (%s) on port (%d)", SERVER, HTTP_PORT); wait(1.0); } ClientGreet(&socket); socket.close(); syslog(LOG_NOTICE, "program end"); } // set mac address void mbed_mac_address(char *mac) { // PEACH1 mac[0] = 0x00; mac[1] = 0x02; mac[2] = 0xF7; mac[3] = 0xF0; mac[4] = 0x00; mac[5] = 0x00; }
void tuvp_tcp_init(void) { mbed_socket::init_sockets(); // _eth.init(); // this will make dynamic address // or set static address // _eth.init("IP Addrss", "Mask", "Gateway"); #if defined (MBED_IP_ADDRESS) _eth.init(MBED_IP_ADDRESS, MBED_IP_MASK, MBED_IP_GATEWAY); #else _eth.init(); #endif _eth.connect(); //TDDDLOG(".. MBED Board IP Address is %s", _eth.getIPAddress()); lwipv4_socket_init(); }
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); } } }
void app_start(int argc, char *argv[]) { (void)argc; (void)argv; MBED_HOSTTEST_TIMEOUT(20); MBED_HOSTTEST_SELECT(tcpecho_client_auto); MBED_HOSTTEST_DESCRIPTION(TCP echo client); MBED_HOSTTEST_START("NET_4"); socket_error_t err = lwipv4_socket_init(); TEST_EQ(err, SOCKET_ERROR_NONE); memset(buffer, 0, sizeof(buffer)); port = 0; s_ip_address ip_addr = {0, 0, 0, 0}; printf("TCPClient waiting for server IP and port..." NL); scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port); printf("Address received:%d.%d.%d.%d:%d" NL, ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port); eth.init(); //Use DHCP eth.connect(); printf("TCPClient IP Address is %s" NL, eth.getIPAddress()); sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4); client = new TCPEchoClient(SOCKET_STACK_LWIP_IPV4); { FunctionPointer2<void, char *, uint16_t> fp(client, &TCPEchoClient::start_test); minar::Scheduler::postCallback(fp.bind(buffer, port)); } }
void app_start(int argc, char *argv[]) { (void) argc; (void) argv; MBED_HOSTTEST_TIMEOUT(5); MBED_HOSTTEST_SELECT(default); MBED_HOSTTEST_DESCRIPTION(Socket Abstraction Layer construction and utility test); MBED_HOSTTEST_START("SAL_INIT_UTIL"); int tests_pass = 1; int rc; EthernetInterface eth; /* Initialise with DHCP, connect, and start up the stack */ eth.init(); eth.connect(); do { socket_error_t err = lwipv4_socket_init(); if (!TEST_EQ(err,SOCKET_ERROR_NONE)) { tests_pass = 0; break; } rc = socket_api_test_create_destroy(SOCKET_STACK_LWIP_IPV4, SOCKET_AF_INET6); tests_pass = tests_pass && rc; rc = socket_api_test_socket_str2addr(SOCKET_STACK_LWIP_IPV4, SOCKET_AF_INET6); tests_pass = tests_pass && rc; } while (0); MBED_HOSTTEST_RESULT(tests_pass); }
int main() { printf("{{start}}\r\n"); EthernetInterface eth; /* Initialise with DHCP, connect, and start up the stack */ eth.init(); eth.connect(); lwipv4_socket_init(); printf("UDP client IP Address is %s\r\n", eth.getIPAddress()); /* Get the current time */ UDPGetTime gt; { FunctionPointer1<void, const char*> fp(>, &UDPGetTime::startGetTime); minar::Scheduler::postCallback(fp.bind(HTTP_SERVER_NAME)); } minar::Scheduler::start(); printf("UDP: %lu seconds since 01/01/1900 00:00 GMT\r\n", gt.time()); eth.disconnect(); float years = (float) gt.time() / 60 / 60 / 24 / 365; printf("{{%s}}\r\n",(years < YEARS_TO_PASS ?"failure":"success")); printf("{{end}}\r\n"); return 0; }
int main() { EthernetInterface eth; NTPClient ntp; eth.init(); //Use DHCP eth.connect(); // NTP set time { bool result = true; const char *url_ntp_server = "0.pool.ntp.org"; printf("NTP_SETTIME: Trying to update time... \r\n"); const int ret = ntp.setTime(url_ntp_server); if (ret == 0) { time_t ctTime = time(NULL); printf("NTP_SETTIME: UTC Time read successfully ... [OK]\r\n"); printf("NTP_SETTIME: %s\r\n", ctime(&ctTime)); } else { printf("NTP_SETTIME: Error(%d) ... [FAIL]\r\n", ret); result = false; } if (result == false) { notify_completion(false); exit(ret); } } eth.disconnect(); notify_completion(true); return 0; }
int main() { MBED_HOSTTEST_TIMEOUT(15); MBED_HOSTTEST_SELECT(default_auto); MBED_HOSTTEST_DESCRIPTION(HTTP client hello world); MBED_HOSTTEST_START("NET_7"); char http_request_buffer[BUFFER_SIZE + 1] = {0}; HTTPClient http; EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); //GET data { bool result = true; const char *url_hello_txt = "http://developer.mbed.org/media/uploads/donatien/hello.txt"; printf("HTTP_GET: Trying to fetch page '%s'...\r\n", url_hello_txt); HTTPResult ret = http.get(url_hello_txt, http_request_buffer, BUFFER_SIZE); if (ret == HTTP_OK) { printf("HTTP_GET: Read %d chars: '%s' ... [OK]\r\n", strlen(http_request_buffer), http_request_buffer); } else { printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode()); result = false; } if (result == false) { eth.disconnect(); MBED_HOSTTEST_RESULT(false); } } //POST data { bool result = true; const char *url_httpbin_post = "http://httpbin.org/post"; HTTPText text(http_request_buffer, BUFFER_SIZE); HTTPMap map; map.put("Hello", "World"); map.put("test", "1234"); printf("HTTP_POST: Trying to post data to '%s' ...\r\n", url_httpbin_post); HTTPResult ret = http.post(url_httpbin_post, map, &text); if (ret == HTTP_OK) { printf("HTTP_POST: Read %d chars ... [OK]\r\n", strlen(http_request_buffer)); printf("HTTP_POST: %s\r\n", http_request_buffer); } else { printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode()); result = false; } if (result == false) { eth.disconnect(); MBED_HOSTTEST_RESULT(false); } } eth.disconnect(); MBED_HOSTTEST_RESULT(true); }
void app_start(int /*argc*/, char* /*argv*/[]) { //Sets the console baud-rate output.baud(115200); // MAC address handling mbed_mac_address(mmac); // This sets up the network interface configuration which will be used // by LWM2M Client API to communicate with mbed Device server. //eth.init("192.168.1.248","255.255.255.0","192.168.1.1"); eth.init(); //Use DHCP eth.connect(); lwipv4_socket_init(); output.printf("IP address %s\r\n", eth.getIPAddress()); // On press of SW3 button on K64F board, example application // will call unregister API towards mbed Device Server unreg_button.fall(&mbed_client,&MbedClient::test_unregister); // On press of SW2 button on K64F board, example application // will send observation towards mbed Device Server obs_button.fall(&mbed_client,&MbedClient::update_resource); // Create LWM2M Client API interface to manage register and unregister mbed_client.create_interface(); // Create LWM2M server object specifying mbed device server // information. M2MSecurity* register_object = mbed_client.create_register_object(); // Create LWM2M device object specifying device resources // as per OMA LWM2M specification. M2MDevice* device_object = mbed_client.create_device_object(); // Create Generic object specifying custom resources M2MObject* generic_object = mbed_client.create_generic_object(); // Create LED Object M2MObject* led_object = mbed_client.create_led_object(); // Add all the objects that you would like to register // into the list and pass the list for register API. M2MObjectList object_list; object_list.push_back(device_object); object_list.push_back(generic_object); object_list.push_back(led_object); mbed_client.set_register_object(register_object); // Issue register command. FunctionPointer2<void, M2MSecurity*, M2MObjectList> fp(&mbed_client, &MbedClient::test_register); minar::Scheduler::postCallback(fp.bind(register_object,object_list)); minar::Scheduler::postCallback(&mbed_client,&MbedClient::test_update_register).period(minar::milliseconds(25000)); }
void app_start(int argc, char *argv[]) { (void) argc; (void) argv; eth.init(); //Use DHCP eth.connect(); lwipv4_socket_init(); printf("MBED: Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT); mbed::FunctionPointer1<void, uint16_t> fp(&server, &TCPEchoServer::start); minar::Scheduler::postCallback(fp.bind(ECHO_SERVER_PORT)); }
void setupEthernet() { lcd.printf("Setup Ethernet...\r\n"); // Starting eth.init(); // Initialize while (eth.connect()) { // timeout and loop until connected lcd.printf("Connect timeout\r\n"); }; lcd.printf("IP Address is %s\r\n", eth.getIPAddress()); // successful connect }
void ethSetup() { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); udp.init(); udp.bind(5683); udp.set_blocking(false, 10000); }
int main() { char buffer[BUFFER_SIZE] = {0}; char out_buffer[BUFFER_SIZE] = {0}; s_ip_address ip_addr = {0, 0, 0, 0}; int port = 0; printf("MBED: TCPCllient waiting for server IP and port...\r\n"); scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port); printf("MBED: Address received: %d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port); EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("MBED: TCPClient IP Address is %s\r\n", eth.getIPAddress()); sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4); TCPSocketConnection socket; while (socket.connect(buffer, port) < 0) { printf("MBED: TCPCllient unable to connect to %s:%d\r\n", buffer, port); wait(1); } // Test loop for multiple client connections bool result = true; int count_error = 0; for (int i = 0; i < MAX_ECHO_LOOPS; i++) { std::generate(out_buffer, out_buffer + BUFFER_SIZE, char_rand); socket.send_all(out_buffer, BUFFER_SIZE); int n = socket.receive(buffer, BUFFER_SIZE); if (n > 0) { bool echoed = memcmp(out_buffer, buffer, BUFFER_SIZE) == 0; result = result && echoed; if (echoed == false) { count_error++; // Count error messages } } } printf("MBED: Loop messages passed: %d / %d\r\n", MAX_ECHO_LOOPS - count_error, MAX_ECHO_LOOPS); if (notify_completion_str(result, buffer)) { socket.send_all(buffer, strlen(buffer)); } socket.close(); eth.disconnect(); notify_completion(result); return 0; }
void app_start(int /*argc*/, char* /*argv*/[]) { //Sets the console baud-rate output.baud(115200); output.printf("In app_start()\r\n"); // This sets up the network interface configuration which will be used // by LWM2M Client API to communicate with mbed Device server. eth.init(); //Use DHCP if (eth.connect() != 0) { output.printf("Failed to form a connection!\r\n"); } if (lwipv4_socket_init() != 0) { output.printf("Error on lwipv4_socket_init!\r\n"); } output.printf("IP address %s\r\n", eth.getIPAddress()); output.printf("Device name %s\r\n", MBED_ENDPOINT_NAME); // we create our button and LED resources auto state_resource = new StateResource(); doorIndicator.onStateChange(mbed::util::FunctionPointer1<void, DoorIndicator::WarnStatus>(state_resource, &StateResource::handle_state_change)); // Unregister button (SW3) press will unregister endpoint from connector.mbed.com unreg_button.fall(&mbed_client, &MbedClient::test_unregister); // Create endpoint interface to manage register and unregister mbed_client.create_interface(); // Create Objects of varying types, see simpleclient.h for more details on implementation. M2MSecurity* register_object = mbed_client.create_register_object(); // server object specifying connector info M2MDevice* device_object = mbed_client.create_device_object(); // device resources object // Create list of Objects to register M2MObjectList object_list; // Add objects to list object_list.push_back(device_object); object_list.push_back(state_resource->get_object()); // Set endpoint registration object mbed_client.set_register_object(register_object); mbed_client.on_object_registered(mbed::util::FunctionPointer0<void>(&doorIndicator, &DoorIndicator::init)); // Issue register command. FunctionPointer2<void, M2MSecurity*, M2MObjectList> fp(&mbed_client, &MbedClient::test_register); minar::Scheduler::postCallback(fp.bind(register_object,object_list)); minar::Scheduler::postCallback(&mbed_client,&MbedClient::test_update_register).period(minar::milliseconds(25000)); }
int main() { eth.init(); eth.connect(); printf("IP Address: %s\n", eth.getIPAddress()); while (true) { int response = m2xClient.postDeviceUpdates(deviceId, 2, streamNames, counts, ats, values); printf("Response code: %d\n", response); if (response == -1) while (true) ; delay(5000); } }
int main(void) { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("Server IP Address is %s:%d\n", eth.getIPAddress(), ECHO_SERVER_PORT); Thread UdpServerTask(udp_server_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25); Thread UdpClientTask(udp_client_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25); // Control TCP server to get mbed statistics { const int TELNET_SERVER_PORT = 23; const int BUFFER_SIZE = 256; TCPSocketServer server; server.bind(TELNET_SERVER_PORT); server.listen(); while (true) { printf("Wait for new connection...\n"); TCPSocketConnection client; server.accept(client); client.set_blocking(false, 1500); // Timeout after (1.5)s printf("Connection from: %s\n", client.get_address()); char buffer[BUFFER_SIZE] = { 0 }; while (true) { int n = client.receive(buffer, sizeof(buffer)); //if (n <= 0) break; if (n > 0) { printf("Recv %d chars\r\n", n); const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n; buffer[buffer_string_end_index] = '\0'; // client.send_all(buffer, strlen(buffer)); if (strncmp(buffer, "stat", 4) == 0) { sprintf(buffer, "received_packets %d\nforwarded_packets %d\nmax_queue_len %d", received_packets, forwarded_packets, max_queue_len); client.send_all(buffer, strlen(buffer)); // printf("%s", buffer); } } //if (n <= 0) break; } client.close(); } } }
/** * The main loop of the TCP Echo Server example * @return 0; however the main loop should never return. */ int main (void) { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); lwipv4_socket_init(); printf("MBED: Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT); TCPEchoServer server; { mbed::FunctionPointer1<void, uint16_t> fp(&server, &TCPEchoServer::start); minar::Scheduler::postCallback(fp.bind(ECHO_SERVER_PORT)); } minar::Scheduler::start(); return 0; }
int main(void) { (void)printf("Initializing mbed specific things...\r\n"); mbed_log_init(); mbedtime_init(); EthernetInterface eth; if (eth.init() || eth.connect()) { (void)printf("Error initializing EthernetInterface.\r\n"); return -1; } return run_tests(); }
int main() { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); UDPSocket sock; sock.init(); Endpoint multicast_group; multicast_group.set_address(MCAST_GRP, MCAST_PORT); char out_buffer[] = "very important data"; while (true) { printf("Multicast to group: %s\n", MCAST_GRP); sock.sendTo(multicast_group, out_buffer, sizeof(out_buffer)); Thread::wait(1000); } }
int main() { Thread * thread; pc.baud(115200); pc.printf("\r\nStarting Mbed ...\r\n"); //Initialize the LCD pc.printf("Initializing LCD ...\r\n"); init_LCD(); printf("Initializing USB Mass Storage ...\r\n"); printf("Inititalizing ethernet ....\r\n"); eth.init(); // Use DHCP eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); // After initializing the ethernet interface // run it in its own thread printf("Starting blinker thread ...\r\n"); thread = new Thread(led1_thread); // Start the shell printf("Starting debug shell ...\r\n"); shell.addCommand("ls", cmd_ls); shell.addCommand("load", cmd_load); shell.addCommand("mem", cmd_mem); shell.addCommand("sensor", cmd_sensor); shell.start(osPriorityNormal, SHELL_STACK_SIZ, shellStack); printf("Shell now running!\r\n"); printf("Available Memory : %d\r\n", get_mem()); // Do something logical here // other than looping while(1) { printf("Temperature : %d °C\r\n", htu21d.sample_ctemp()); printf("Humitdity : %d%%\r\n", htu21d.sample_humid()); wait(10); } thread->terminate(); delete thread; }
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); } }
HTTPAPI_RESULT HTTPAPI_Init(void) { LogInfo("HTTPAPI_Init::Start\r\n"); time_t ctTime; ctTime = time(NULL); HTTPAPI_RESULT result; LogInfo("HTTAPI_Init::Time is now (UTC) %s\r\n", ctime(&ctTime)); if (eth.init()) { LogInfo("HTTPAPI_Init::Error with initing the Ethernet Interface\r\n"); result = HTTPAPI_INIT_FAILED; } else { LogInfo("HTTPAPI_Init::Ethernet interface was inited!\r\n"); if (eth.connect(30000)) { LogError("HTTPAPI_Init::Error with connecting.\r\n"); result = HTTPAPI_INIT_FAILED; } else { LogInfo("HTTAPI_Init::Ethernet interface was connected (brought up)!\r\n"); LogInfo("HTTAPI_Init::MAC address %s\r\n", eth.getMACAddress()); LogInfo("HTTAPI_Init::IP address %s\r\n", eth.getIPAddress()); if (ntp.setTime("0.pool.ntp.org") == 0) { LogInfo("HTTAPI_Init:: Successfully set time\r\n"); LogInfo("HTTAPI_Init::Time is now (UTC) %s\r\n", ctime(&ctTime)); result = HTTPAPI_OK; } else { LogError("HTTPAPI_INIT::Unable to set time. Init failed"); result = HTTPAPI_INIT_FAILED; } } } LogInfo("HTTPAPI_Init::End\r\n"); return result; }
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; }
void app_start (int argc, char *argv[]) { (void) argc; (void) argv; eth.init(); //Use DHCP eth.connect(); socket_error_t err = lwipv4_socket_init(); if (err) { printf("MBED: Failed to initialize socket stack (%d)\r\n", err); return; } udpserver = new UDPSocket(SOCKET_STACK_LWIP_IPV4); printf("MBED: UDP Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT); udpserver->setOnError(UDPSocket::ErrorHandler_t(onError)); udpserver->open(SOCKET_AF_INET4); err = udpserver->bind("0.0.0.0",ECHO_SERVER_PORT); udpserver->error_check(err); udpserver->setOnReadable(UDPSocket::ReadableHandler_t(onRx)); printf("MBED: Waiting for packet...\r\n"); }
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); } } }
control_t test_echo_udp_client() { socket_error_t err = lwipv4_socket_init(); TEST_ASSERT_EQUAL_MESSAGE(SOCKET_ERROR_NONE, err, "Failed to init LWIPv4 socket!"); printf("MBED: Initializing ethernet connection." NL); //Use DHCP TEST_ASSERT_EQUAL_MESSAGE(0, eth.init(), "Failed to init LWIPv4 socket!"); eth.connect(); printf("MBED: IP Address is %s" NL, eth.getIPAddress()); greentea_send_kv("target_ip", eth.getIPAddress()); memset(buffer, 0, sizeof(buffer)); port = 0; printf("UDPClient waiting for server IP and port..." NL); char recv_key[] = "host_port"; char port_value[] = "65325"; greentea_send_kv("host_ip", " "); TEST_ASSERT_NOT_EQUAL_MESSAGE(0, greentea_parse_kv(recv_key, buffer, sizeof(recv_key), sizeof(buffer)), "MBED: Failed to recv/parse key value from host!"); greentea_send_kv("host_port", " "); TEST_ASSERT_NOT_EQUAL_MESSAGE(0, greentea_parse_kv(recv_key, port_value, sizeof(recv_key), sizeof(port_value)), "MBED: Failed to recv/parse key value from host!"); sscanf(port_value, "%d", &port); client = new UDPEchoClient(SOCKET_STACK_LWIP_IPV4); { mbed::util::FunctionPointer2<void, char *, uint16_t> fp(client, &UDPEchoClient::start_test); minar::Scheduler::postCallback(fp.bind(buffer, port)); } return CaseTimeout(25000); }
void app_start(int /*argc*/, char* /*argv*/[]) { //Sets the console baud-rate output.baud(115200); // start the accelerometer acc.enable(); // print device name for easy reference output.printf("Device Name is '%s'\r\n\r\n",MBED_ENDPOINT_NAME); // This sets up the network interface configuration which will be used // by LWM2M Client API to communicate with mbed Device server. eth.init(); //Use DHCP eth.connect(); output.printf("%s\n", eth.getMACAddress()); char *ip = eth.getIPAddress(); if (ip) { output.printf("IP Address is: %s\n", ip); } else { error("Failed to aquire IP address\n"); } lwipv4_socket_init(); // On press of SW3 button on K64F board, example application // will call unregister API towards mbed Device Server // unreg_button.fall(&mbed_client,&MbedClient::test_unregister); // Create LWM2M Client API interface to manage register and unregister mbed_client.create_interface(); // Create LWM2M server object specifying mbed device server // information. M2MSecurity* register_object = mbed_client.create_register_object(); // Create LWM2M device object specifying device resources // as per OMA LWM2M specification. M2MDevice* device_object = mbed_client.create_device_object(); // Create Generic object specifying custom resources M2MObject* sdw_object = mbed_client.create_sdw_object(); // Create Generic object specifying custom resources M2MObject* led_object = mbed_client.create_led_object(); // Add all the objects that you would like to register // into the list and pass the list for register API. M2MObjectList object_list; object_list.push_back(device_object); object_list.push_back(sdw_object); object_list.push_back(led_object); mbed_client.set_register_object(register_object); // Issue register command. FunctionPointer2<void, M2MSecurity*, M2MObjectList> fp(&mbed_client, &MbedClient::test_register); minar::Scheduler::postCallback(fp.bind(register_object, object_list)); minar::Scheduler::postCallback(&mbed_client, &MbedClient::update_sdw_resource).period(minar::milliseconds(1000)); minar::Scheduler::postCallback(&mbed_client, &MbedClient::test_update_register).period(minar::milliseconds(25000)); }
void app_start(int /*argc*/, char* /*argv*/[]) { #else int main() { #endif // Instantiate the class which implements // mbed Client API MbedClient mbed_client; #ifdef TARGET_LIKE_LINUX pthread_t unregister_thread; pthread_t observation_thread; m2mclient = &mbed_client; trace_init(); set_trace_print_function( trace_printer ); set_trace_config(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_DEBUG|TRACE_CARRIAGE_RETURN); signal(SIGINT, (signalhandler_t)ctrl_c_handle_function); #else // This sets up the network interface configuration which will be used // by LWM2M Client API to communicate with mbed Device server. #ifdef SIXLOWPAN_INTERFACE Mesh6LoWPAN_ND *mesh_api = Mesh6LoWPAN_ND::getInstance(); int8_t status; status = mesh_api->init(rf_device_register(), mesh_network_callback); if (status != MESH_ERROR_NONE) { tr_error("Mesh network initialization failed %d!", status); return 1; } status = mesh_api->connect(); if (status != MESH_ERROR_NONE) { tr_error("Can't connect to mesh network!"); return 1; } do { mesh_api->processEvent(); } while(mesh_network_state != MESH_CONNECTED); #else EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); lwipv4_socket_init(); #endif // Set up Hardware interrupt button. InterruptIn obs_button(OBS_BUTTON); InterruptIn unreg_button(UNREG_BUTTON); // On press of SW3 button on K64F board, example application // will call unregister API towards mbed Device Server unreg_button.fall(&mbed_client,&MbedClient::test_unregister); // On press of SW2 button on K64F board, example application // will send observation towards mbed Device Server obs_button.fall(&mbed_client,&MbedClient::update_resource); #endif // Create LWM2M Client API interface to manage bootstrap, // register and unregister //These 2 are defining the secure connection usage M2MSecurity::SecurityModeType mode = M2MSecurity::NoSecurity; _have_secure_conn = (mode == M2MSecurity::Certificate || mode == M2MSecurity::Psk); // SecurityNotSet, // Psk = 0, // Certificate = 2, // NoSecurity = 3 mbed_client.create_interface(); #ifdef TARGET_LIKE_LINUX #if defined (BOOTSTRAP_ENABLED) pthread_t bootstrap_thread; // Create LWM2M bootstrap object specifying bootstrap server // information. M2MSecurity* security_object = mbed_client.create_bootstrap_object(_have_secure_conn); // Issue bootstrap command. mbed_client.test_bootstrap(security_object); pthread_create(&bootstrap_thread, NULL, &wait_for_bootstrap, (void*) &mbed_client); pthread_join(bootstrap_thread, NULL); #else M2MSecurity *register_object = mbed_client.create_register_object(_have_secure_conn); if( mode == M2MSecurity::Certificate ){ register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); register_object->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len); register_object->set_resource_value(M2MSecurity::PublicKey,cert,cert_len); register_object->set_resource_value(M2MSecurity::Secretkey,key,key_len); }else if( mode == M2MSecurity::Psk ){ register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Psk); register_object->set_resource_value(M2MSecurity::ServerPublicKey,psk_identity,psk_identity_len); register_object->set_resource_value(M2MSecurity::PublicKey,psk_identity,psk_identity_len); register_object->set_resource_value(M2MSecurity::Secretkey,psk,psk_len); }else{ register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); } mbed_client.set_register_object(register_object); M2MDevice* device_object = mbed_client.create_device_object(); M2MObject* object = mbed_client.create_generic_object(); M2MObjectList object_list; object_list.push_back(device_object); object_list.push_back(object); mbed_client.test_register(object_list); #endif pthread_create(&observation_thread, NULL, &send_observation, (void*) &mbed_client); pthread_create(&unregister_thread, NULL, &wait_for_unregister, (void*) &mbed_client); pthread_join(unregister_thread, NULL); #else #if defined (BOOTSTRAP_ENABLED) // Create LWM2M bootstrap object specifying bootstrap server // information. M2MSecurity* security_object = mbed_client.create_bootstrap_object(_have_secure_conn); // Issue bootstrap command. mbed_client.test_bootstrap(security_object); // Wait till the bootstrap callback is called successfully. // Callback comes in bootstrap_done() #ifdef SIXLOWPAN_INTERFACE /* wait network to be established */ do { mesh_interface_run(); } while(!mbed_client.bootstrap_successful()); #else while (!mbed_client.bootstrap_successful()) { __WFI(); } #endif #else M2MSecurity *register_object = mbed_client.create_register_object(_have_secure_conn); if( mode == M2MSecurity::Certificate ){ register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Certificate); register_object->set_resource_value(M2MSecurity::ServerPublicKey,server_cert,server_cert_len); register_object->set_resource_value(M2MSecurity::PublicKey,cert,cert_len); register_object->set_resource_value(M2MSecurity::Secretkey,key,key_len); }else if( mode == M2MSecurity::Psk ){ register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::Psk); register_object->set_resource_value(M2MSecurity::ServerPublicKey,psk_identity,psk_identity_len); register_object->set_resource_value(M2MSecurity::PublicKey,psk_identity,psk_identity_len); register_object->set_resource_value(M2MSecurity::Secretkey,psk,psk_len); }else{ register_object->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity); } mbed_client.set_register_object(register_object); #endif // Create LWM2M device object specifying device resources // as per OMA LWM2M specification. M2MDevice* device_object = mbed_client.create_device_object(); M2MObject* object = mbed_client.create_generic_object(); // Add all the objects that you would like to register // into the list and pass the list for register API. M2MObjectList object_list; object_list.push_back(device_object); object_list.push_back(object); // Issue register command. FunctionPointer1<void, M2MObjectList> fp(&mbed_client, &MbedClient::test_register); minar::Scheduler::postCallback(fp.bind(object_list)); minar::Scheduler::start(); // Wait till the register callback is called successfully. // Callback comes in object_registered() #ifdef SIXLOWPAN_INTERFACE /* wait network to be established */ do { mesh_api->processEvent(); } while(!mbed_client.register_successful()); #endif // Wait for the unregister successful callback, // Callback comes in object_unregsitered(), this will be // waiting for user to press SW2 button on K64F board. #ifdef SIXLOWPAN_INTERFACE /* wait network to be established */ do { mesh_api->processEvent(); } while(!mbed_client.unregister_successful()); #endif #if defined(BOOTSTRAP_ENABLED) // This will turn on the LED on the board specifying that // the application has run successfully. notify_completion(mbed_client.unregister_successful() && mbed_client.register_successful() && mbed_client.bootstrap_successful()); // Delete security object created for bootstrapping if(security_object) { delete security_object; } #else // Disconnect the connect and teardown the network interface #ifdef SIXLOWPAN_INTERFACE mesh_api->disconnect(); #else eth.disconnect(); #endif #endif //BOOTSTRAP_ENABLED // Delete device object created for registering device // resources. if(device_object) { M2MDevice::delete_instance(); } if(object) { delete object; } #endif //TARGET_LIKE_LINUX #ifndef TARGET_LIKE_MBED return 0; #endif }
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