HAPSRG / UsaClient

Files at this revision

API Documentation at this revision

Comitter:
cocorlow
Date:
Sun Oct 24 15:15:58 2021 +0000
Commit message:
UsaClient first

Changed in this revision

UsaClient.cpp Show annotated file Show diff for this revision Revisions of this file
UsaClient.hpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UsaClient.cpp	Sun Oct 24 15:15:58 2021 +0000
@@ -0,0 +1,50 @@
+#include "UsaClient.hpp"
+
+UsaClient::UsaClient(EthernetInterface* _net)
+{
+    net = _net;
+    sock.open(net);
+}
+
+UsaClient::UsaClient(EthernetInterface* _net, const char* ip, int port)
+{
+    net = _net;
+    sock.open(net);
+    socketAddress.set_ip_address(ip);
+    socketAddress.set_port(port);
+}
+
+int UsaClient::Write(int address, const void* data, int size)
+{
+    nsapi_size_or_error_t e;
+    write_buffer.address = (uint32_t)address;
+    write_buffer.rw = 0x00;
+    write_buffer.option = 0x00;
+    write_buffer.size = (uint16_t)size;
+    uint8_t* p = (uint8_t*)data;
+    for (int i = 0; i < size; i++, p++)
+    {
+        write_buffer.data[i] = *p;
+    }
+    e = sock.sendto(socketAddress, &write_buffer, 8 + size);
+    return (int)e;
+}
+
+int UsaClient::Read(int address, const void* data, int size)
+{
+    nsapi_size_or_error_t e;
+    SocketAddress server_address;
+    read_buffer.address = (uint32_t)address;
+    read_buffer.rw = 0x01;
+    read_buffer.option = 0x00;
+    read_buffer.size = (uint16_t)size;
+    e = sock.sendto(socketAddress, &read_buffer, 8);
+    
+    e = sock.recvfrom(&server_address, &read_buffer, 8 + size);
+    uint8_t* p = (uint8_t*)data;
+    for (int i = 0; i < size; i++, p++)
+    {
+        *p = read_buffer.data[i];
+    }
+    return (int)e;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/UsaClient.hpp	Sun Oct 24 15:15:58 2021 +0000
@@ -0,0 +1,34 @@
+#ifndef __USACLIENT_HPP__
+#define __USACLIENT_HPP__
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+#define USACLIENT_BUFFER_MAX 1500
+
+
+class UsaClient
+{
+private:
+    EthernetInterface* net;
+    UDPSocket sock;
+    struct buffer
+    {
+        uint32_t address;
+        uint8_t rw;
+        uint8_t option;
+        uint16_t size;
+        uint8_t data[USACLIENT_BUFFER_MAX];
+    };
+    buffer write_buffer;
+    buffer read_buffer;
+public:
+    SocketAddress socketAddress;
+    
+    UsaClient(EthernetInterface* _net);
+    UsaClient(EthernetInterface* _net, const char* ip, int port);
+    int Write(int address, const void* data, int size);
+    int Read(int address, const void* data, int size);
+};
+
+#endif
\ No newline at end of file