Skip to content

Commit 467a31f

Browse files
committed
Add C027Interface
1 parent a5800ef commit 467a31f

File tree

3 files changed

+381
-0
lines changed

3 files changed

+381
-0
lines changed

net/C027Interface/C027Interface.cpp

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/* C027 implementation of NetworkInterfaceAPI
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "C027Interface.h"
18+
#include "mbed.h"
19+
20+
21+
// C027Interface implementation
22+
C027Interface::C027Interface(const char *simpin, bool debug)
23+
: _debug(debug)
24+
{
25+
strcpy(_pin, simpin);
26+
}
27+
28+
int C027Interface::connect(const char *apn, const char *username, const char *password)
29+
{
30+
// create the modem
31+
_mdm = new MDMSerial;
32+
if (_debug) {
33+
_mdm->setDebug(4);
34+
} else {
35+
_mdm->setDebug(0);
36+
}
37+
38+
// initialize the modem
39+
MDMParser::DevStatus devStatus = {};
40+
MDMParser::NetStatus netStatus = {};
41+
bool mdmOk = _mdm->init(_pin, &devStatus);
42+
if (_debug) {
43+
_mdm->dumpDevStatus(&devStatus);
44+
}
45+
46+
if (mdmOk) {
47+
// wait until we are connected
48+
mdmOk = _mdm->registerNet(&netStatus);
49+
if (_debug) {
50+
_mdm->dumpNetStatus(&netStatus);
51+
}
52+
}
53+
54+
if (mdmOk) {
55+
// join the internet connection
56+
MDMParser::IP ip = _mdm->join(apn, username, password);
57+
_ip_address.set_ip_bytes(&ip, NSAPI_IPv4);
58+
mdmOk = (ip != NOIP);
59+
}
60+
61+
return mdmOk ? 0 : NSAPI_ERROR_DEVICE_ERROR;
62+
}
63+
64+
int C027Interface::disconnect()
65+
{
66+
if (!_mdm->disconnect()) {
67+
return NSAPI_ERROR_DEVICE_ERROR;
68+
}
69+
70+
return 0;
71+
}
72+
73+
const char *C027Interface::get_ip_address()
74+
{
75+
return _ip_address.get_ip_address();
76+
}
77+
78+
const char *C027Interface::get_mac_address()
79+
{
80+
return 0;
81+
}
82+
83+
struct c027_socket {
84+
int socket;
85+
MDMParser::IpProtocol proto;
86+
MDMParser::IP ip;
87+
int port;
88+
};
89+
90+
int C027Interface::socket_open(void **handle, nsapi_protocol_t proto)
91+
{
92+
MDMParser::IpProtocol mdmproto = (proto == NSAPI_UDP) ? MDMParser::IPPROTO_UDP : MDMParser::IPPROTO_TCP;
93+
int fd = _mdm->socketSocket(mdmproto);
94+
if (fd < 0) {
95+
return NSAPI_ERROR_NO_SOCKET;
96+
}
97+
98+
_mdm->socketSetBlocking(fd, 10000);
99+
struct c027_socket *socket = new struct c027_socket;
100+
if (!socket) {
101+
return NSAPI_ERROR_NO_SOCKET;
102+
}
103+
104+
socket->socket = fd;
105+
socket->proto = mdmproto;
106+
*handle = socket;
107+
return 0;
108+
}
109+
110+
int C027Interface::socket_close(void *handle)
111+
{
112+
struct c027_socket *socket = (struct c027_socket *)handle;
113+
_mdm->socketFree(socket->socket);
114+
115+
delete socket;
116+
return 0;
117+
}
118+
119+
int C027Interface::socket_bind(void *handle, const SocketAddress &address)
120+
{
121+
return NSAPI_ERROR_UNSUPPORTED;
122+
}
123+
124+
int C027Interface::socket_listen(void *handle, int backlog)
125+
{
126+
return NSAPI_ERROR_UNSUPPORTED;
127+
}
128+
129+
int C027Interface::socket_connect(void *handle, const SocketAddress &addr)
130+
{
131+
struct c027_socket *socket = (struct c027_socket *)handle;
132+
133+
if (!_mdm->socketConnect(socket->socket, addr.get_ip_address(), addr.get_port())) {
134+
return NSAPI_ERROR_DEVICE_ERROR;
135+
}
136+
137+
return 0;
138+
}
139+
140+
int C027Interface::socket_accept(void **handle, void *server)
141+
{
142+
return NSAPI_ERROR_UNSUPPORTED;
143+
}
144+
145+
int C027Interface::socket_send(void *handle, const void *data, unsigned size)
146+
{
147+
struct c027_socket *socket = (struct c027_socket *)handle;
148+
149+
int sent = _mdm->socketSend(socket->socket, (const char *)data, size);
150+
if (sent == SOCKET_ERROR) {
151+
return NSAPI_ERROR_DEVICE_ERROR;
152+
}
153+
154+
return sent;
155+
}
156+
157+
int C027Interface::socket_recv(void *handle, void *data, unsigned size)
158+
{
159+
struct c027_socket *socket = (struct c027_socket *)handle;
160+
if (!_mdm->socketReadable(socket->socket)) {
161+
return NSAPI_ERROR_WOULD_BLOCK;
162+
}
163+
164+
int recv = _mdm->socketRecv(socket->socket, (char *)data, size);
165+
if (recv == SOCKET_ERROR) {
166+
return NSAPI_ERROR_DEVICE_ERROR;
167+
}
168+
169+
return recv;
170+
}
171+
172+
int C027Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
173+
{
174+
struct c027_socket *socket = (struct c027_socket *)handle;
175+
176+
int sent = _mdm->socketSendTo(socket->socket,
177+
*(MDMParser::IP *)addr.get_ip_bytes(), addr.get_port(),
178+
(const char *)data, size);
179+
180+
if (sent == SOCKET_ERROR) {
181+
return NSAPI_ERROR_DEVICE_ERROR;
182+
}
183+
184+
return sent;
185+
}
186+
187+
int C027Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
188+
{
189+
struct c027_socket *socket = (struct c027_socket *)handle;
190+
if (!_mdm->socketReadable(socket->socket)) {
191+
return NSAPI_ERROR_WOULD_BLOCK;
192+
}
193+
194+
MDMParser::IP ip;
195+
int port;
196+
197+
int recv = _mdm->socketRecvFrom(socket->socket, &ip, &port, (char *)data, size);
198+
if (recv == SOCKET_ERROR) {
199+
return NSAPI_ERROR_DEVICE_ERROR;
200+
}
201+
202+
if (addr) {
203+
addr->set_ip_bytes(&ip, NSAPI_IPv4);
204+
addr->set_port(port);
205+
}
206+
207+
return recv;
208+
}
209+
210+
void C027Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
211+
{
212+
}

net/C027Interface/C027Interface.h

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/* C027 implementation of NetworkInterfaceAPI
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef C027_INTERFACE_H
18+
#define C027_INTERFACE_H
19+
20+
#include "CellularInterface.h"
21+
#include "MDM.h"
22+
23+
24+
/** C027Interface class
25+
* Implementation of the NetworkInterface for C027
26+
*/
27+
class C027Interface : public NetworkStack, public CellularInterface
28+
{
29+
public:
30+
/** C027Interfacelifetime
31+
* @param simpin Optional PIN for the SIM
32+
* @param debug Enable debugging
33+
*/
34+
C027Interface(const char *simpin=0, bool debug=false);
35+
36+
/** Start the interface
37+
*
38+
* @param apn Optional name of the network to connect to
39+
* @param username Optional username for your APN
40+
* @param password Optional password for your APN
41+
* @return 0 on success, negative error code on failure
42+
*/
43+
virtual int connect(const char *apn = 0, const char *username = 0, const char *password = 0);
44+
45+
/** Stop the interface
46+
* @return 0 on success, negative on failure
47+
*/
48+
virtual int disconnect();
49+
50+
/** Get the internally stored IP address
51+
* @return IP address of the interface or null if not yet connected
52+
*/
53+
virtual const char *get_ip_address();
54+
55+
/** Get the internally stored MAC address
56+
* @return MAC address of the interface
57+
*/
58+
virtual const char *get_mac_address();
59+
60+
protected:
61+
/** Open a socket
62+
* @param handle Handle in which to store new socket
63+
* @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
64+
* @return 0 on success, negative on failure
65+
*/
66+
virtual int socket_open(void **handle, nsapi_protocol_t proto);
67+
68+
/** Close the socket
69+
* @param handle Socket handle
70+
* @return 0 on success, negative on failure
71+
* @note On failure, any memory associated with the socket must still
72+
* be cleaned up
73+
*/
74+
virtual int socket_close(void *handle);
75+
76+
/** Bind a server socket to a specific port
77+
* @param handle Socket handle
78+
* @param address Local address to listen for incoming connections on
79+
* @return 0 on success, negative on failure.
80+
*/
81+
virtual int socket_bind(void *handle, const SocketAddress &address);
82+
83+
/** Start listening for incoming connections
84+
* @param handle Socket handle
85+
* @param backlog Number of pending connections that can be queued up at any
86+
* one time [Default: 1]
87+
* @return 0 on success, negative on failure
88+
*/
89+
virtual int socket_listen(void *handle, int backlog);
90+
91+
/** Connects this TCP socket to the server
92+
* @param handle Socket handle
93+
* @param address SocketAddress to connect to
94+
* @return 0 on success, negative on failure
95+
*/
96+
virtual int socket_connect(void *handle, const SocketAddress &address);
97+
98+
/** Accept a new connection.
99+
* @param handle Handle in which to store new socket
100+
* @param server Socket handle to server to accept from
101+
* @return 0 on success, negative on failure
102+
* @note This call is not-blocking, if this call would block, must
103+
* immediately return NSAPI_ERROR_WOULD_WAIT
104+
*/
105+
virtual int socket_accept(void **handle, void *server);
106+
107+
/** Send data to the remote host
108+
* @param handle Socket handle
109+
* @param data The buffer to send to the host
110+
* @param size The length of the buffer to send
111+
* @return Number of written bytes on success, negative on failure
112+
* @note This call is not-blocking, if this call would block, must
113+
* immediately return NSAPI_ERROR_WOULD_WAIT
114+
*/
115+
virtual int socket_send(void *handle, const void *data, unsigned size);
116+
117+
/** Receive data from the remote host
118+
* @param handle Socket handle
119+
* @param data The buffer in which to store the data received from the host
120+
* @param size The maximum length of the buffer
121+
* @return Number of received bytes on success, negative on failure
122+
* @note This call is not-blocking, if this call would block, must
123+
* immediately return NSAPI_ERROR_WOULD_WAIT
124+
*/
125+
virtual int socket_recv(void *handle, void *data, unsigned size);
126+
127+
/** Send a packet to a remote endpoint
128+
* @param handle Socket handle
129+
* @param address The remote SocketAddress
130+
* @param data The packet to be sent
131+
* @param size The length of the packet to be sent
132+
* @return the number of written bytes on success, negative on failure
133+
* @note This call is not-blocking, if this call would block, must
134+
* immediately return NSAPI_ERROR_WOULD_WAIT
135+
*/
136+
virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
137+
138+
/** Receive a packet from a remote endpoint
139+
* @param handle Socket handle
140+
* @param address Destination for the remote SocketAddress or null
141+
* @param buffer The buffer for storing the incoming packet data
142+
* If a packet is too long to fit in the supplied buffer,
143+
* excess bytes are discarded
144+
* @param size The length of the buffer
145+
* @return the number of received bytes on success, negative on failure
146+
* @note This call is not-blocking, if this call would block, must
147+
* immediately return NSAPI_ERROR_WOULD_WAIT
148+
*/
149+
virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
150+
151+
/** Register a callback on state change of the socket
152+
* @param handle Socket handle
153+
* @param callback Function to call on state change
154+
* @param data Argument to pass to callback
155+
* @note Callback may be called in an interrupt context.
156+
*/
157+
virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
158+
159+
private:
160+
// Modem object
161+
bool _debug;
162+
MDMSerial *_mdm;
163+
SocketAddress _ip_address;
164+
char _mac_address[NSAPI_MAC_SIZE];
165+
char _pin[sizeof "1234"];
166+
};
167+
168+
#endif

net/C027Interface/C027_Support.lib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://developer.mbed.org/teams/NetworkSocketAPI/code/C027_Support/#596bcb599ac1

0 commit comments

Comments
 (0)