int lwm2m_client_bootstrap_object_command(int argc, char *argv[]) { int return_code = CMDLINE_RETCODE_FAIL; char *address = 0; if( cmd_parameter_val(argc, argv, "--address", &address) ){ if(lwm2m_client.create_bootstrap_object(address)){ return_code = CMDLINE_RETCODE_SUCCESS; } } else { return_code = CMDLINE_RETCODE_INVALID_PARAMETERS; } return return_code; }
static nsapi_size_or_error_t udp_sendto_command_handler(SInfo *info, int argc, char *argv[]) { char *host; int32_t port; if (!cmd_parameter_val(argc, argv, "sendto", &host)) { cmd_printf("Need host name\r\n"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } if (!cmd_parameter_int(argc, argv, host, &port)) { cmd_printf("Need port number\r\n"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } // Replace NULL-strings with NULL host = strcmp(host, "NULL") ? host : NULL; int32_t len; void *data; if (cmd_parameter_int(argc, argv, "--data_len", &len)) { data = malloc(len); if (!data) { cmd_printf("Failed to allocate memory\r\n"); return CMDLINE_RETCODE_FAIL; } } else { // Replace NULL-strings with NULL if (strcmp(argv[5], "NULL") == 0) { data = NULL; len = 0; } else { data = argv[5]; len = strlen(argv[5]); } } SocketAddress addr(NULL, port); nsapi_size_or_error_t ret = get_interface()->gethostbyname(host, &addr); if (ret) { return handle_nsapi_size_or_error("NetworkInterface::gethostbyname()", ret); } ret = info->socket().sendto(addr, data, len); if (ret > 0) { cmd_printf("sent: %d bytes\r\n", ret); } if (data != argv[5]) { free(data); } return handle_nsapi_size_or_error("Socket::sendto()", ret); }
int lwm2m_client_register_object_command(int argc, char *argv[]) { char *address = 0; cmd_parameter_val(argc, argv, "--address", &address); int useSecureConn = 0; if( !cmd_parameter_int(argc, argv, "--secure", &useSecureConn) ){ useSecureConn = 0; } if(lwm2m_client.create_register_object(address, useSecureConn == 1)) { return CMDLINE_RETCODE_SUCCESS; } return CMDLINE_RETCODE_INVALID_PARAMETERS; }
int lwm2m_client_set_value_command(int argc, char *argv[]) { int return_code = CMDLINE_RETCODE_INVALID_PARAMETERS; char *name = 0; int32_t value = 0; int32_t object_instance = 0; cmd_parameter_int(argc, argv, "--object_instance", &object_instance); if(cmd_parameter_val(argc, argv, "--name", &name) && cmd_parameter_int(argc, argv, "--value", &value)) { if(lwm2m_client.set_resource_value(name,value,object_instance)) { return_code = CMDLINE_RETCODE_SUCCESS; } } return return_code; }
int lwm2m_client_dynamic_resource_instance_command(int argc, char *argv[]) { int return_code = CMDLINE_RETCODE_INVALID_PARAMETERS; char *name = 0; int32_t multiple_instance = 0; int32_t object_instance = 0; int32_t resource_instance = 0; int32_t observable = 0; int32_t resource_instance_operation = 1; int32_t value_type = -1; cmd_parameter_int(argc, argv, "--value_type", &value_type); cmd_parameter_int(argc, argv, "--multiple_instance", &multiple_instance); cmd_parameter_int(argc, argv, "--object_instance", &object_instance); cmd_parameter_int(argc, argv, "--resource_instance", &resource_instance); cmd_parameter_int(argc, argv, "--observable", &observable); cmd_parameter_int(argc, argv, "--resource_instance_operation", &resource_instance_operation); if(-1 == value_type){ value_type = 1; } if(cmd_parameter_val(argc, argv, "--name", &name)) { if(0 == value_type){ if(lwm2m_client.create_dynamic_resource_instance_string(name,observable, multiple_instance, object_instance, resource_instance, resource_instance_operation)) { return_code = CMDLINE_RETCODE_SUCCESS; } } else if(1 == value_type){ if(lwm2m_client.create_dynamic_resource_instance_int(name,observable, multiple_instance, object_instance, resource_instance, resource_instance_operation)) { return_code = CMDLINE_RETCODE_SUCCESS; } } } return return_code; }
int lwm2m_client_device_command(int argc, char *argv[]) { int return_code = CMDLINE_RETCODE_SUCCESS;// CMDLINE_RETCODE_INVALID_PARAMETERS; char *manufacturer = 0; char *model_number = 0; char *serial_number = 0; char *device_type = 0; char *hardware_version = 0; char *software_version = 0; char *firmware_version = 0; char *utc_offset = 0; char *timezone = 0; int32_t current_time = 0; int32_t available_power_sources = 0; int32_t power_source_voltage = 0; int32_t power_source_current = 0; int32_t battery_status = 0; int32_t battery_level = 0; int32_t memory_free = 0; int32_t memory_total = 0; int32_t error_code = 0; int32_t instance_id = 0; lwm2m_client.create_device_object(); if(cmd_parameter_val(argc, argv, "--manufacturer", &manufacturer)) { if(!lwm2m_client.create_device_object(M2MDevice::Manufacturer, manufacturer)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_val(argc, argv, "--model_number", &model_number)) { if(!lwm2m_client.create_device_object(M2MDevice::ModelNumber, model_number)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_val(argc, argv, "--serial_number", &serial_number)){ if(!lwm2m_client.create_device_object(M2MDevice::SerialNumber, serial_number)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_val(argc, argv, "--device_type", &device_type)){ if(!lwm2m_client.create_device_object(M2MDevice::DeviceType, device_type)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_val(argc, argv, "--hardware_version", &hardware_version)){ if(!lwm2m_client.create_device_object(M2MDevice::HardwareVersion, hardware_version)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_val(argc, argv, "--software_version", &software_version)){ if(!lwm2m_client.create_device_object(M2MDevice::SoftwareVersion, software_version)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_val(argc, argv, "--firmware_version", &firmware_version)){ if(!lwm2m_client.create_device_object(M2MDevice::FirmwareVersion, firmware_version)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_int(argc, argv, "--available_power_sources", &available_power_sources)){ if(cmd_parameter_int(argc, argv, "--instance_id", &instance_id)) { if(!lwm2m_client.create_device_object(M2MDevice::AvailablePowerSources, available_power_sources,instance_id)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } } if(cmd_parameter_int(argc, argv, "--power_source_voltage", &power_source_voltage)){ if(cmd_parameter_int(argc, argv, "--instance_id", &instance_id)) { if(!lwm2m_client.create_device_object(M2MDevice::PowerSourceVoltage, power_source_voltage,instance_id)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } } if(cmd_parameter_int(argc, argv, "--power_source_current", &power_source_current)){ if(cmd_parameter_int(argc, argv, "--instance_id", &instance_id)) { if(!lwm2m_client.create_device_object(M2MDevice::PowerSourceCurrent, power_source_current,instance_id)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } } if(cmd_parameter_int(argc, argv, "--battery_level", &battery_level)){ if(!lwm2m_client.create_device_object(M2MDevice::BatteryLevel, battery_level)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_int(argc, argv, "--battery_status", &battery_status)){ if(!lwm2m_client.create_device_object(M2MDevice::BatteryStatus, battery_status)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_int(argc, argv, "--memory_free", &memory_free)){ if(!lwm2m_client.create_device_object(M2MDevice::MemoryFree, memory_free)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_int(argc, argv, "--memory_total", &memory_total)){ if(!lwm2m_client.create_device_object(M2MDevice::MemoryTotal, memory_total)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_int(argc, argv, "--error_code", &error_code)){ if(cmd_parameter_int(argc, argv, "--instance_id", &instance_id)) { if(!lwm2m_client.create_device_object(M2MDevice::ErrorCode, error_code,instance_id)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } } if(cmd_parameter_int(argc, argv, "--current_time", ¤t_time)){ if(!lwm2m_client.create_device_object(M2MDevice::CurrentTime, current_time)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_val(argc, argv, "--utc_offset", &utc_offset)){ if(!lwm2m_client.create_device_object(M2MDevice::UTCOffset, utc_offset)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } if(cmd_parameter_val(argc, argv, "--timezone", &timezone)){ if(!lwm2m_client.create_device_object(M2MDevice::Timezone, timezone)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } } return return_code; }
int lwm2m_client_setup_command(int argc, char *argv[]) { char *endpoint = 0; char *type = 0; int lifetime = -1; int32_t port = 5683; char *domain = 0; int32_t binding_mode = 1; int32_t network_interface = 1; if (!cmd_parameter_val(argc, argv, "--endpoint", &endpoint)) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } int opt_params = 0; cmd_parameter_val(argc, argv, "--type", &type); if (cmd_parameter_int(argc, argv, "--lifetime", &lifetime)) { if (opt_params != 0) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } else { opt_params += 1; } } if (cmd_parameter_int(argc, argv, "--port", &port)) { if (port > UINT16_MAX || opt_params != 1) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } else { opt_params += 1; } } if (cmd_parameter_val(argc, argv, "--domain", &domain)) { if (opt_params != 2) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } else { opt_params += 1; } } if (cmd_parameter_int(argc, argv, "--binding_mode", &binding_mode)) { if (opt_params != 3) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } else { opt_params += 1; } } if (cmd_parameter_int(argc, argv, "--network_interface", &network_interface)) { if (opt_params != 4) { return CMDLINE_RETCODE_INVALID_PARAMETERS; } else { opt_params += 1; } } bool success = false; if (lwm2m_client.create_interface(opt_params, endpoint, type, lifetime, port, domain, binding_mode, network_interface)) { return CMDLINE_RETCODE_SUCCESS; } return CMDLINE_RETCODE_FAIL; }
static int cmd_socket(int argc, char *argv[]) { if (cmd_parameter_index(argc, argv, "new") == 1) { return cmd_socket_new(argc, argv); } else if (cmd_parameter_index(argc, argv, "print-mode") > 0) { if (cmd_parameter_index(argc, argv, "--string") > 0) { printing_mode = PRINT_STRING; } else if (cmd_parameter_index(argc, argv, "--hex") > 0) { printing_mode = PRINT_HEX; } else if (cmd_parameter_index(argc, argv, "--disabled") > 0) { printing_mode = PRINT_DISABLED; } int32_t parsed_col_width = 0; if (cmd_parameter_int(argc, argv, "--col-width", &parsed_col_width)) { if (parsed_col_width <= 0) { cmd_printf("Printing column width must be > 0"); return CMDLINE_RETCODE_FAIL; } if (printing_mode == PRINT_HEX && parsed_col_width > 42) { cmd_printf("Maximum column width for hex data is 42 bytes"); return CMDLINE_RETCODE_FAIL; } printing_col_width = (int)parsed_col_width; } // Allow print-mode to be used as a parameter to other commands if (cmd_parameter_index(argc, argv, "print-mode") == 1) { return CMDLINE_RETCODE_SUCCESS; } } // Rest of the commands require Socket SInfo *info = get_sinfo(strtol(argv[1], NULL, 10)); if (!info) { cmd_printf("Invalid socket id %s\r\n", argv[1]); return CMDLINE_RETCODE_FAIL; } bool enable_pattern_check; if (cmd_parameter_bool(argc, argv, "set_RFC_864_pattern_check", &enable_pattern_check)) { info->set_pattern_check(enable_pattern_check); return CMDLINE_RETCODE_SUCCESS; } // Helper macro for checking the which command was given #define COMMAND_IS(cmd) (cmd_parameter_index(argc, argv, cmd) == 2) /* * Generic Socket commands: * delete, open, close, bind, set_blocking, set_timeout */ if (COMMAND_IS("delete")) { return del_sinfo(info); } else if (COMMAND_IS("open")) { NetworkInterface *interface; interface = get_interface(); // get default interface if (!interface) { cmd_printf("Invalid interface\r\n"); return CMDLINE_RETCODE_FAIL; } switch (info->type()) { case SInfo::IP: return handle_nsapi_error("Socket::open()", info->internetsocket()->open(interface)); case SInfo::TCP_SERVER: return handle_nsapi_error("TCPServer::open()", info->tcp_server()->open(interface)); #if defined(MBEDTLS_SSL_CLI_C) case SInfo::TLS: return handle_nsapi_error("Socket::open()", info->tls_socket()->open(interface)); #endif default: cmd_printf("Not a IP socket\r\n"); return CMDLINE_RETCODE_FAIL; } } else if (COMMAND_IS("close")) { return handle_nsapi_error("Socket::close()", info->socket().close()); } else if (COMMAND_IS("bind")) { int32_t port = 0; char *addr; if (!cmd_parameter_int(argc, argv, "port", &port) && !cmd_parameter_int(argc, argv, "bind", &port) && port <= 0 && port > 65535) { printf("Missing or invalid port number\n"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } if (cmd_parameter_val(argc, argv, "addr", &addr)) { // Replace NULL-strings with NULL addr = strcmp(addr, "NULL") ? addr : NULL; cmd_printf("Socket::bind(%s, %" PRId32 ")\r\n", addr, port); SocketAddress tmp(addr, port); return handle_nsapi_error("Socket::bind(addr, port)", info->socket().bind(tmp)); } else { cmd_printf("Socket::bind(%" PRId32 ")\r\n", port); SocketAddress tmp(NULL, port); return handle_nsapi_error("Socket::bind(port)", info->socket().bind(tmp)); } } else if (COMMAND_IS("set_blocking")) { bool val; if (!cmd_parameter_bool(argc, argv, "set_blocking", &val)) { cmd_printf("Need boolean value"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } info->set_blocking(val); return CMDLINE_RETCODE_SUCCESS; } else if (COMMAND_IS("set_timeout")) { int32_t ms; if (!cmd_parameter_int(argc, argv, "set_timeout", &ms)) { cmd_printf("Need timeout value"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } if (ms == -1) { info->set_blocking(true); } else { info->set_blocking(false); } info->socket().set_timeout(ms); return CMDLINE_RETCODE_SUCCESS; } else if (COMMAND_IS("register_sigio_cb")) { info->socket().sigio(callback(sigio_handler, info)); return CMDLINE_RETCODE_SUCCESS; } /* * Commands related to UDPSocket: * sendto, recvfrom */ if (COMMAND_IS("sendto")) { return udp_sendto_command_handler(info, argc, argv); } else if (COMMAND_IS("recvfrom")) { return udp_recvfrom_command_handler(info, argc, argv); } else if (COMMAND_IS("start_udp_receiver_thread")) { return start_udp_receiver_thread(info, argc, argv); } else if (COMMAND_IS("last_data_received")) { print_data((const uint8_t *)info->getReceiveBuffer(), info->getDataCount()); if (info->getPacketSizeArray()) { int *packetSizes = info->getPacketSizeArray(); cmd_printf("packet_sizes: "); for (int i = 0; i < PACKET_SIZE_ARRAY_LEN; i++) { cmd_printf("%d ", packetSizes[i]); } cmd_printf("\r\n"); } if (info->getReceiverThread()) { info->getReceiverThread()->terminate(); } thread_clean_up(info); return handle_nsapi_error("Socket::last_data_received()", NSAPI_ERROR_OK); } /* * Commands related to TCPSocket, TLSSocket * connect, send, recv */ if (COMMAND_IS("connect")) { char *host; int32_t port; if (!cmd_parameter_val(argc, argv, "connect", &host)) { cmd_printf("Need host name\r\n"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } if (!cmd_parameter_int(argc, argv, host, &port)) { cmd_printf("Need port number\r\n"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } if (strcmp(host, "NULL") == 0) { host = NULL; } cmd_printf("Host name: %s port: %" PRId32 "\r\n", host, port); if (info->type() == SInfo::IP) { SocketAddress addr(NULL, port); nsapi_error_t ret = get_interface()->gethostbyname(host, &addr); if (ret) { return handle_nsapi_error("NetworkInterface::gethostbyname()", ret); } return handle_nsapi_error("Socket::connect()", info->socket().connect(addr)); #if defined(MBEDTLS_SSL_CLI_C) } else if (info->type() == SInfo::TLS) { return handle_nsapi_error("TLSSocket::connect()", static_cast<TLSSocket &>(info->socket()).connect(host, port)); #endif } } else if (COMMAND_IS("send")) { return tcp_send_command_handler(info, argc, argv); } else if (COMMAND_IS("recv")) { return tcp_recv_command_handler(info, argc, argv); } else if (COMMAND_IS("start_tcp_receiver_thread")) { return start_tcp_receiver_thread(info, argc, argv); } else if (COMMAND_IS("join_tcp_receiver_thread")) { info->getReceiverThread()->join(); print_data((const uint8_t *)info->getReceiveBuffer(), info->getDataCount()); cmd_printf("received: %d bytes\r\n", info->getRecvTotal()); thread_clean_up(info); return CMDLINE_RETCODE_SUCCESS; } else if (COMMAND_IS("start_bg_traffic_thread")) { return start_bg_traffic_thread(info, argc, argv); } else if (COMMAND_IS("join_bg_traffic_thread")) { int bg_thread_success = CMDLINE_RETCODE_SUCCESS; if (!info->available()) { // Tells that background thread stumbled to an issue and stopped prematurely bg_thread_success = CMDLINE_RETCODE_FAIL; } info->setUnavailable(); info->getReceiverThread()->join(); thread_clean_up(info); return bg_thread_success; } else if (COMMAND_IS("setsockopt_keepalive")) { int32_t seconds; nsapi_error_t ret; if (!cmd_parameter_int(argc, argv, "setsockopt_keepalive", &seconds)) { cmd_printf("Need keep-alive value(0-7200seconds)"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } ret = info->socket().setsockopt(NSAPI_SOCKET, NSAPI_KEEPALIVE, &seconds, sizeof(seconds)); return handle_nsapi_error("Socket::setsockopt()", ret); } else if (COMMAND_IS("getsockopt_keepalive")) { int32_t optval; unsigned optlen = sizeof(optval); nsapi_error_t ret; ret = info->socket().getsockopt(NSAPI_SOCKET, NSAPI_KEEPALIVE, &optval, &optlen); if (optlen != sizeof(int)) { return CMDLINE_RETCODE_FAIL; } if (ret < 0) { return handle_nsapi_error("Socket::getsockopt()", ret); } return handle_nsapi_size_or_error("Socket::getsockopt()", optval); } /* * Commands for TCPServer * listen, accept */ if (COMMAND_IS("listen")) { int32_t backlog; if (cmd_parameter_int(argc, argv, "listen", &backlog)) { return handle_nsapi_error("TCPServer::listen()", info->socket().listen(backlog)); } else { return handle_nsapi_error("TCPServer::listen()", info->socket().listen()); } } else if (COMMAND_IS("accept")) { nsapi_error_t ret; if (info->type() != SInfo::TCP_SERVER) { Socket *new_sock = info->socket().accept(&ret); if (ret == NSAPI_ERROR_OK) { SInfo *new_info = new SInfo(new_sock, false); m_sockets.push_back(new_info); cmd_printf("Socket::accept() new socket sid: %d\r\n", new_info->id()); } return handle_nsapi_error("Socket::accept()", ret); } else { // Old TCPServer API int32_t id; SocketAddress addr; if (!cmd_parameter_int(argc, argv, "accept", &id)) { cmd_printf("Need new socket id\r\n"); return CMDLINE_RETCODE_INVALID_PARAMETERS; } SInfo *new_info = get_sinfo(id); if (!new_info) { cmd_printf("Invalid socket id\r\n"); return CMDLINE_RETCODE_FAIL; } TCPSocket *new_sock = static_cast<TCPSocket *>(&new_info->socket()); nsapi_error_t ret = static_cast<TCPServer &>(info->socket()).accept(new_sock, &addr); if (ret == NSAPI_ERROR_OK) { cmd_printf("TCPServer::accept() new socket sid: %d connection from %s port %d\r\n", new_info->id(), addr.get_ip_address(), addr.get_port()); } return handle_nsapi_error("TCPServer::accept()", ret); } } /* * Commands for TLSSocket * set_root_ca_cert */ #if defined(MBEDTLS_SSL_CLI_C) if (COMMAND_IS("set_root_ca_cert")) { if (info->type() != SInfo::TLS) { cmd_printf("Not a TLS socket.\r\n"); return CMDLINE_RETCODE_FAIL; } return handle_nsapi_error("TLSSocket::tls_set_cert", tls_set_cert(argc, argv, info)); } #endif return CMDLINE_RETCODE_INVALID_PARAMETERS; }