#ifdef __linux__ #include "ubxlib.h" #ifdef U_CFG_PPP_ENABLE #include #include #include #include #include #include #include #include #include #ifndef U_CFG_DISABLE_TEST_AUTOMATION #include "issam_LTE.h" #endif #define MY_SERVER_NAME "ubxlib.com" #define MY_SERVER_PORT 5055 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; struct hostent *pHostEnt; struct sockaddr_in destinationAddress = {0}; int32_t sock; const char message[] = "The quick brown linux-fox jumps over the lazy dog."; size_t txSize = sizeof(message) - 1; // exclude null-terminator for transmission char buffer[128]; size_t rxSize = 0; int32_t returnCode; struct ifreq interface = {0}; // Initialize the APIs we will need uPortInit(); uDeviceInit(); // Open the device returnCode = uDeviceOpen(&gDeviceCfg, &devHandle); printf("Opened device with return code %d.\n", returnCode); if (returnCode == 0) { // Bring up the network interface printf("Bringing up the network...\n"); if (uNetworkInterfaceUp(devHandle, U_NETWORK_TYPE_CELL, &gNetworkCfg) == 0) { // Linux is now connected to the internet via pppd and the cellular module // Look up the IP address of the echo server errno = 0; pHostEnt = gethostbyname(MY_SERVER_NAME); if (pHostEnt != NULL) { printf("Found %s at %s.\n", MY_SERVER_NAME, inet_ntoa(*(struct in_addr *) pHostEnt->h_addr)); destinationAddress.sin_addr = *(struct in_addr *) pHostEnt->h_addr; destinationAddress.sin_family = pHostEnt->h_addrtype; destinationAddress.sin_port = htons(MY_SERVER_PORT); sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); if (sock >= 0) { snprintf(interface.ifr_name, sizeof(interface.ifr_name), "ppp0"); if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, &interface, sizeof(interface)) == 0) { if (connect(sock, (struct sockaddr *) &destinationAddress, sizeof(destinationAddress)) == 0) { if (send(sock, message, txSize, 0) == txSize) { printf("Sent %zu byte(s) to echo server.\n", txSize); rxSize = recv(sock, buffer, sizeof(buffer) - 1, 0); if (rxSize > 0) { buffer[rxSize] = '\0'; // Ensure null-termination printf("\nReceived echo back (%zu byte(s)): %s\n", rxSize, buffer); } else { printf("\nNo reply received!\n"); } } else { printf("Unable to send to server (errno %d)!\n", errno); } } else { printf("Unable to connect to server (errno %d)!\n", errno); } } else { printf("Unable to bind socket to interface ppp0 (errno %d)!\n", errno); } // Close the socket printf("Closing socket...\n"); shutdown(sock, SHUT_RDWR); close(sock); } else { printf("Unable to create socket (errno %d)!\n", errno); } } else { printf("Unable to find %s (errno %d)!\n", MY_SERVER_NAME, errno); } // When finished with the network layer printf("Taking down network...\n"); uNetworkInterfaceDown(devHandle, U_NETWORK_TYPE_CELL); } else { printf("Unable to bring up the network!\n"); } // Close the device printf("Closing device...\n"); uDeviceClose(devHandle, false); } else { printf("Unable to open the device (return code %d)!\n", returnCode); } // Tidy up printf("Deinitializing ubxlib...\n"); uDeviceDeinit(); uPortDeinit(); printf("Done.\n"); return 0; // Return success } #endif // #ifdef U_CFG_PPP_ENABLE #endif // #ifdef linux