#ifdef __linux__ #include "ubxlib.h" #ifdef U_CFG_PPP_ENABLE #include #include #include #include #include #include #include #include "string.h" // For strlen() #include "stdio.h" // For snprintf() #include "issam_LTE.h" /* ---------------------------------------------------------------- * COMPILE-TIME MACROS * -------------------------------------------------------------- */ // HTTPS server URL: this is a test server that accepts PUT/POST // requests and GET/HEAD/DELETE requests on port 8081; there is // also an HTTP server on port 8080. #define MY_SERVER_NAME "ubxlib.com:8080" // Some data to PUT and GET with the server. const char *gpMyData = "Hello world!"; static const uNetworkType_t gNetType = U_NETWORK_TYPE_CELL; static const uDeviceCfg_t gDeviceCfg = { .deviceType = U_DEVICE_TYPE_CELL, .deviceCfg = { .cfgCell = { .moduleType = U_CELL_MODULE_TYPE_LENA_R8, .pSimPinCode = NULL, .pinEnablePower = U_CFG_APP_PIN_CELL_ENABLE_POWER, .pinPwrOn = U_CFG_APP_PIN_CELL_PWR_ON, .pinVInt = U_CFG_APP_PIN_CELL_VINT, .pinDtrPowerSaving = U_CFG_APP_PIN_CELL_DTR }, }, .transportType = U_DEVICE_TRANSPORT_TYPE_UART, .transportCfg = { .cfgUart = { .uart = 0, // UART port number .pPrefix = "/dev/ttyUSB", // Prefix for the device file .baudRate = U_CELL_UART_BAUD_RATE, .pinTxd = U_CFG_APP_PIN_CELL_TXD, .pinRxd = U_CFG_APP_PIN_CELL_RXD, .pinCts = U_CFG_APP_PIN_CELL_CTS, .pinRts = U_CFG_APP_PIN_CELL_RTS, #ifdef U_CFG_APP_UART_PREFIX .pPrefix = U_PORT_STRINGIFY_QUOTED(U_CFG_APP_UART_PREFIX) #else .pPrefix = NULL #endif }, }, }; static const uNetworkCfgCell_t gNetworkCfg = { .type = U_NETWORK_TYPE_CELL, .pApn = "TM", .timeoutSeconds = 240 }; int main() { uDeviceHandle_t devHandle = NULL; uHttpClientContext_t *pContext = NULL; uHttpClientConnection_t connection = U_HTTP_CLIENT_CONNECTION_DEFAULT; uSecurityTlsSettings_t tlsSettings = U_SECURITY_TLS_SETTINGS_DEFAULT; char serialNumber[U_SECURITY_SERIAL_NUMBER_MAX_LENGTH_BYTES]; char path[U_SECURITY_SERIAL_NUMBER_MAX_LENGTH_BYTES + 10]; char buffer[32] = {0}; size_t size = sizeof(buffer); int32_t statusCode = 0; int32_t returnCode; // Initialise the APIs we will need uPortInit(); uDeviceInit(); // Open the device returnCode = uDeviceOpen(&gDeviceCfg, &devHandle); uPortLog("Opened device with return code %d.\n", returnCode); if (returnCode == 0) { // Bring up the network interface uPortLog("Bringing up the network...\n"); if (uNetworkInterfaceUp(devHandle, gNetType, &gNetworkCfg) == 0) { // Note: since devHandle is a cellular // handle, any of the `cell` API calls could // be made here using it. // In order to create a unique path on the public // server which won't collide with anyone else, // we make the path the serial number of the // module uSecurityGetSerialNumber(devHandle, serialNumber); snprintf(path, sizeof(path), "/%s.html", serialNumber); // Set the URL of the server; each instance // is associated with a single server - // you may create more than one insance, // for different servers, or close and // open instances to access more than one // server. There are other settings in // uHttpClientConnection_t, but for the // purposes of this example they can be // left at defaults connection.pServerName = MY_SERVER_NAME; // Create an HTTPS instance for the server; to // create an HTTP instance instead you would // replace &tlsSettings with NULL (and of // course use port 8080 on the test HTTP server). pContext = pUHttpClientOpen(devHandle, &connection, &tlsSettings); if (pContext != NULL) { // POST some data to the server; doesn't have to be text, // can be anything, including binary data, though obviously // you must give the appropriate content-type statusCode = uHttpClientPostRequest(pContext, path, gpMyData, strlen(gpMyData), "text/plain", NULL, NULL, NULL); if (statusCode == 200) { uPortLog("POST some data to the file \"%s\" on %s.\n", path, MY_SERVER_NAME); // GET it back again statusCode = uHttpClientGetRequest(pContext, path, buffer, &size, NULL); if (statusCode == 200) { uPortLog("GET the data: it was \"%s\" (%d byte(s)).\n", buffer, size); } else { uPortLog("Unable to GET file \"%s\" from %s; status code was %d!\n", path, MY_SERVER_NAME, statusCode); } } else { uPortLog("Unable to POST file \"%s\" to %s; status code was %d!\n", path, MY_SERVER_NAME, statusCode); } // Close the HTTP instance again uHttpClientClose(pContext); } else { uPortLog("Unable to create HTTP instance!\n"); } // When finished with the network layer uPortLog("Taking down network...\n"); uNetworkInterfaceDown(devHandle, gNetType); } else { uPortLog("Unable to bring up the network!\n"); } // Close the device // Note: we don't power the device down here in order // to speed up testing; you may prefer to power it off // by setting the second parameter to true. uDeviceClose(devHandle, false); } else { uPortLog("Unable to bring up the device!\n"); } // Tidy up uDeviceDeinit(); uPortDeinit(); uPortLog("Done.\n"); } #endif // #ifdef U_CFG_PPP_ENABLE #endif // #ifdef linux