• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2014 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  *    http://www.eclipse.org/legal/epl-v10.html
10  * and the Eclipse Distribution License is available at
11  *   http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  *    Allan Stockdill-Mander - initial API and implementation and/or initial documentation
15  *******************************************************************************/
16 
17 #include "MQTTCC3200.h"
18 
19 unsigned long MilliTimer;
20 
SysTickIntHandler(void)21 void SysTickIntHandler(void) {
22 	MilliTimer++;
23 }
24 
expired(Timer * timer)25 char expired(Timer* timer) {
26 	long left = timer->end_time - MilliTimer;
27 	return (left < 0);
28 }
29 
30 
countdown_ms(Timer * timer,unsigned int timeout)31 void countdown_ms(Timer* timer, unsigned int timeout) {
32 	timer->end_time = MilliTimer + timeout;
33 }
34 
35 
countdown(Timer * timer,unsigned int timeout)36 void countdown(Timer* timer, unsigned int timeout) {
37 	timer->end_time = MilliTimer + (timeout * 1000);
38 }
39 
40 
left_ms(Timer * timer)41 int left_ms(Timer* timer) {
42 	long left = timer->end_time - MilliTimer;
43 	return (left < 0) ? 0 : left;
44 }
45 
46 
InitTimer(Timer * timer)47 void InitTimer(Timer* timer) {
48 	timer->end_time = 0;
49 }
50 
51 
cc3200_read(Network * n,unsigned char * buffer,int len,int timeout_ms)52 int cc3200_read(Network* n, unsigned char* buffer, int len, int timeout_ms) {
53 	SlTimeval_t timeVal;
54 	SlFdSet_t fdset;
55 	int rc = 0;
56 	int recvLen = 0;
57 
58 	SL_FD_ZERO(&fdset);
59 	SL_FD_SET(n->my_socket, &fdset);
60 
61 	timeVal.tv_sec = 0;
62 	timeVal.tv_usec = timeout_ms * 1000;
63 	if (sl_Select(n->my_socket + 1, &fdset, NULL, NULL, &timeVal) == 1) {
64 		do {
65 			rc = sl_Recv(n->my_socket, buffer + recvLen, len - recvLen, 0);
66 			recvLen += rc;
67 		} while(recvLen < len);
68 	}
69 	return recvLen;
70 }
71 
72 
cc3200_write(Network * n,unsigned char * buffer,int len,int timeout_ms)73 int cc3200_write(Network* n, unsigned char* buffer, int len, int timeout_ms) {
74 	SlTimeval_t timeVal;
75 	SlFdSet_t fdset;
76 	int rc = 0;
77 	int readySock;
78 
79 	SL_FD_ZERO(&fdset);
80 	SL_FD_SET(n->my_socket, &fdset);
81 
82 	timeVal.tv_sec = 0;
83 	timeVal.tv_usec = timeout_ms * 1000;
84 	do {
85 		readySock = sl_Select(n->my_socket + 1, NULL, &fdset, NULL, &timeVal);
86 	} while(readySock != 1);
87 	rc = sl_Send(n->my_socket, buffer, len, 0);
88 	return rc;
89 }
90 
91 
cc3200_disconnect(Network * n)92 void cc3200_disconnect(Network* n) {
93 	sl_Close(n->my_socket);
94 }
95 
96 
NewNetwork(Network * n)97 void NewNetwork(Network* n) {
98 	n->my_socket = 0;
99 	n->mqttread = cc3200_read;
100 	n->mqttwrite = cc3200_write;
101 	n->disconnect = cc3200_disconnect;
102 }
103 
TLSConnectNetwork(Network * n,char * addr,int port,SlSockSecureFiles_t * certificates,unsigned char sec_method,unsigned int cipher,char server_verify)104 int TLSConnectNetwork(Network *n, char* addr, int port, SlSockSecureFiles_t* certificates, unsigned char sec_method, unsigned int cipher, char server_verify) {
105 	SlSockAddrIn_t sAddr;
106 	int addrSize;
107 	int retVal;
108 	unsigned long ipAddress;
109 
110 	retVal = sl_NetAppDnsGetHostByName(addr, strlen(addr), &ipAddress, AF_INET);
111 	if (retVal < 0) {
112 		return -1;
113 	}
114 
115 	sAddr.sin_family = AF_INET;
116 	sAddr.sin_port = sl_Htons((unsigned short)port);
117 	sAddr.sin_addr.s_addr = sl_Htonl(ipAddress);
118 
119 	addrSize = sizeof(SlSockAddrIn_t);
120 
121 	n->my_socket = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET);
122 	if (n->my_socket < 0) {
123 		return -1;
124 	}
125 
126 	SlSockSecureMethod method;
127 	method.secureMethod = sec_method;
128 	retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECMETHOD, &method, sizeof(method));
129 	if (retVal < 0) {
130 		return retVal;
131 	}
132 
133 	SlSockSecureMask mask;
134 	mask.secureMask = cipher;
135 	retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &mask, sizeof(mask));
136 	if (retVal < 0) {
137 		return retVal;
138 	}
139 
140 	if (certificates != NULL) {
141 		retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECURE_FILES, certificates->secureFiles, sizeof(SlSockSecureFiles_t));
142 		if(retVal < 0)
143 		{
144 			return retVal;
145 		}
146 	}
147 
148 	retVal = sl_Connect(n->my_socket, ( SlSockAddr_t *)&sAddr, addrSize);
149 	if( retVal < 0 ) {
150 		if (server_verify || retVal != -453) {
151 			sl_Close(n->my_socket);
152 			return retVal;
153 		}
154 	}
155 
156 	SysTickIntRegister(SysTickIntHandler);
157 	SysTickPeriodSet(80000);
158 	SysTickEnable();
159 
160 	return retVal;
161 }
162 
ConnectNetwork(Network * n,char * addr,int port)163 int ConnectNetwork(Network* n, char* addr, int port)
164 {
165 	SlSockAddrIn_t sAddr;
166 	int addrSize;
167 	int retVal;
168 	unsigned long ipAddress;
169 
170 	sl_NetAppDnsGetHostByName(addr, strlen(addr), &ipAddress, AF_INET);
171 
172 	sAddr.sin_family = AF_INET;
173 	sAddr.sin_port = sl_Htons((unsigned short)port);
174 	sAddr.sin_addr.s_addr = sl_Htonl(ipAddress);
175 
176 	addrSize = sizeof(SlSockAddrIn_t);
177 
178 	n->my_socket = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
179 	if( n->my_socket < 0 ) {
180 		// error
181 		return -1;
182 	}
183 
184 	retVal = sl_Connect(n->my_socket, ( SlSockAddr_t *)&sAddr, addrSize);
185 	if( retVal < 0 ) {
186 		// error
187 		sl_Close(n->my_socket);
188 	    return retVal;
189 	}
190 
191 	SysTickIntRegister(SysTickIntHandler);
192 	SysTickPeriodSet(80000);
193 	SysTickEnable();
194 
195 	return retVal;
196 }
197