ppp for lpc1768 with phs bridge
Dependencies: AbitModemInterface mbed-rtos mbed
Revision 0:fab2fa3c525b, committed 2015-07-01
- Comitter:
- phsfan
- Date:
- Wed Jul 01 01:01:10 2015 +0000
- Commit message:
- test build;
Changed in this revision
diff -r 000000000000 -r fab2fa3c525b AbitModemInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AbitModemInterface.lib Wed Jul 01 01:01:10 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/phsfan/code/AbitModemInterface/#f5bcfb224067
diff -r 000000000000 -r fab2fa3c525b TinyHTTP.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TinyHTTP.cpp Wed Jul 01 01:01:10 2015 +0000 @@ -0,0 +1,124 @@ +/* + * mbed Tiny HTTP Client for Ethernet Interface Library + * Copyright (c) 2012 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ + +/** @file + * @brief Tiny HTTP Client + */ + +#include "mbed.h" +#include "TinyHTTP.h" +#include "TCPSocketConnection.h" +#include <ctype.h> + +//#define DEBUG + +// Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) +int base64enc(const char *input, unsigned int length, char *output, int len) { + static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + unsigned int c, c1, c2, c3; + + if (len < ((((length-1)/3)+1)<<2)) return -1; + for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) { + c1 = ((((unsigned char)*((unsigned char *)&input[i])))); + c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0; + c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0; + + c = ((c1 & 0xFC) >> 2); + output[j+0] = base64[c]; + c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4); + output[j+1] = base64[c]; + c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6); + output[j+2] = (length>i+1)?base64[c]:'='; + c = (c3 & 0x3F); + output[j+3] = (length>i+2)?base64[c]:'='; + } + output[(((length-1)/3)+1)<<2] = '\0'; + return 0; +} + +// Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) +int urlencode(char *str, char *buf, int len) { + static const char to_hex[] = "0123456789ABCDEF"; +// char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; + char *pstr = str, *pbuf = buf; + + if (len < (strlen(str) * 3 + 1)) return -1; + while (*pstr) { + if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') { + *pbuf++ = *pstr; + } else if (*pstr == ' ') { + *pbuf++ = '+'; + } else { + *pbuf++ = '%'; + *pbuf++ = to_hex[(*pstr >> 4) & 0x0f]; + *pbuf++ = to_hex[*pstr & 0x0f]; + } + pstr++; + } + *pbuf = '\0'; + return 0; +} + +void createauth (char *user, char *pwd, char *buf, int len) { + char tmp[80]; + + strncpy(buf, "Authorization: Basic ", len); + snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd); + base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf)); + strncat(buf, "\r\n", len - strlen(buf)); +} + +int httpRequest (int method, char *host, int port, char *uri, char *head, char *body, onHttpReceiveFunc func) { + char buf[1500]; + int i, ret = -1; + + TCPSocketConnection socket; + if (socket.connect(host, port) < 0) goto exit; + +#ifdef DEBUG + printf("connected\r\n"); +#endif + + // send request + if (method == METHOD_POST) { + socket.send_all("POST ", 5); + } else { + socket.send_all("GET ", 4); + } + socket.send_all(uri, strlen(uri)); + socket.send_all(" HTTP/1.1\r\nHost: ", 17); + socket.send_all(host, strlen(host)); + socket.send_all("\r\n", 2); + socket.send_all("application/x-www-form-urlencoded\r\n", 35); + socket.send_all("Connection: close\r\n", 19); + if (head) { + socket.send_all(head, strlen(head)); + } + if (method == METHOD_POST) { + sprintf(buf, "Content-Length: %d\r\n", strlen(body)); + socket.send_all(buf, strlen(buf)); + } + socket.send_all("\r\n", 2); + + // post method + if (method == METHOD_POST && body) { + socket.send_all(body, strlen(body)); + } + + // recv + for (;;) { + i = socket.receive(buf, sizeof(buf) - 1); + if (i == 0) break; + + if (func != NULL) func(buf, i); + } + ret = 0; + +exit: + socket.close(); + + return ret; +}
diff -r 000000000000 -r fab2fa3c525b TinyHTTP.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TinyHTTP.h Wed Jul 01 01:01:10 2015 +0000 @@ -0,0 +1,40 @@ +/* + * mbed Tiny HTTP Client for Ethernet Interface Library + * Copyright (c) 2012 Hiroshi Suga + * Released under the MIT License: http://mbed.org/license/mit + */ + +/** @file + * @brief Tiny HTTP Client + */ + +#ifndef TinyHTTP_h +#define TinyHTTP_h + +#include "mbed.h" + +#define HTTP_PORT 80 +#define HTTP_TIMEOUT 3000 // ms + +#define METHOD_GET 0 +#define METHOD_POST 1 + +typedef void (*onHttpReceiveFunc)(char *buf, int len); + +/** send http request + * @param method METHOD_GET or METHOD_POST + * @param host http server + * @param uri URI + * @param head http header (CR+LF) (or NULL) + * @param body POST body (or NULL) + * @return http code, -1:failue + */ +int httpRequest (int method, char *host, int port, char *uri, char *head, char *body, onHttpReceiveFunc func); + +void createauth (char *user, char *pwd, char *buf, int len); + +int base64enc(const char *input, unsigned int length, char *output, int len); + +int urlencode(char *str, char *buf, int len); + +#endif
diff -r 000000000000 -r fab2fa3c525b main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jul 01 01:01:10 2015 +0000 @@ -0,0 +1,90 @@ +#include "mbed.h" +#include "AbitModemInterface.h" +#include <new> +#include "TinyHTTP.h" + +//#define BAUD 120000 +#define BAUD 9600 + +Serial pc(USBTX, USBRX); +DigitalOut rts(p11); + +void no_memory () { + error("no_memory\r\n"); + exit(-1); +} + +void onrecv (char *buf, int len) { + int i; + pc.printf("recv: "); + for (i = 0; i < len; i ++) { + pc.putc(buf[i]); + } +} + +void ppp(void const*) // PPP +{ + AbitModemInterface modem(p13, p14, NC, NC, p12, BAUD); // tx, rx, cts, rtx, reset + + printf("connect\r\n"); + int ret = modem.connect("prin", "prin"); + if(ret) { + printf("Could not connect\r\n"); + return; + } + + printf("IP Address %s\r\n", modem.getIPAddress()); + + //GET data + printf("Trying to fetch page...\r\n"); + httpRequest(METHOD_GET, "developer.mbed.org", 80, "/media/uploads/phsfan/hello.txt", NULL, NULL, &onrecv); + printf("\r\n"); + + Thread::wait(5000); + + printf("exit\r\n"); + modem.disconnect(); +} + +void sm(void const*) // short mail (SMS) +{ + AbitModemInterface modem(p13, p14, NC, NC, p12, BAUD); // tx, rx, cts, rtx, reset + + modem.sendSM("07012345678", "Hello"); + + while(true) + { + char num[17]; + char msg[64]; + + int ret = modem.getSM(num, msg, 64); + if (!ret) { + printf("%s : %s\n", num, msg); + } + + Thread::wait(3000); + } +} + +int main() { + set_new_handler(no_memory); + + pc.baud(115200); + pc.printf("** PHS\r\n"); + Thread::wait(1000); + rts = 0; + + Thread testTask(ppp, NULL, osPriorityNormal, 1024 * 4); +// Thread smTask(sm, NULL, osPriorityNormal, 1024 * 4); + DigitalOut led(LED1); + while(1) + { + led=!led; + Thread::wait(1000); + } +} + +extern "C" +void HardFault_Handler() { + error("Hard Fault!\r\n"); +}
diff -r 000000000000 -r fab2fa3c525b mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Wed Jul 01 01:01:10 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#ef0a22cdf839
diff -r 000000000000 -r fab2fa3c525b mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jul 01 01:01:10 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0 \ No newline at end of file