r4479 - in developers/matt_hsu: . agps-online
matt_hsu at docs.openmoko.org
matt_hsu at docs.openmoko.org
Fri Jun 6 13:37:47 CEST 2008
Author: matt_hsu
Date: 2008-06-06 13:37:46 +0200 (Fri, 06 Jun 2008)
New Revision: 4479
Added:
developers/matt_hsu/agps-online/
developers/matt_hsu/agps-online/Makefile
developers/matt_hsu/agps-online/agps-onlinec.c
developers/matt_hsu/agps-online/agps-onlinec.h
developers/matt_hsu/agps-online/ubx.c
developers/matt_hsu/agps-online/ubx.h
Log:
tiny agps client
Added: developers/matt_hsu/agps-online/Makefile
===================================================================
--- developers/matt_hsu/agps-online/Makefile (rev 0)
+++ developers/matt_hsu/agps-online/Makefile 2008-06-06 11:37:46 UTC (rev 4479)
@@ -0,0 +1,11 @@
+# simple Makefile
+
+CC=arm-angstrom-linux-gnueabi-gcc
+#CC=gcc
+
+.PHONY: all clean
+
+all:
+ $(CC) agps-onlinec.c ubx.c -g -o agps-onlinec
+clean:
+ rm -f agps-onlinec
Added: developers/matt_hsu/agps-online/agps-onlinec.c
===================================================================
--- developers/matt_hsu/agps-online/agps-onlinec.c (rev 0)
+++ developers/matt_hsu/agps-online/agps-onlinec.c 2008-06-06 11:37:46 UTC (rev 4479)
@@ -0,0 +1,249 @@
+/*
+ * CopyRight (c) 2008 by OpenMoko, Inc.
+ * Matt Hsu <matt_hsu at openmoko.org>
+ * tiny client to obtain A-GPS data
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <termios.h>
+#include <string.h>
+
+#include "agps-onlinec.h"
+#include "ubx.h"
+
+#define CONTENT_LENGTH "Content-Length: "
+#define CONTENT_TYPE "Content-Type: "
+#define UBX_FORMAT "application/ubx"
+
+#define BUFFER_LEN 2048
+
+/* AGPS server information */
+static const char *ubx_agps_ser = "agps.u-blox.com";
+static const int ser_port = 46434;
+
+static char agps_buf[4096];
+struct agps_onlinec_data agps;
+
+static const char *key[] = {
+ "cmd=", "user=", "pwd=",
+ "lat=", "lon=", "alt=", "pacc=",
+ "latency=",
+};
+
+static void usage(void)
+{
+ puts("Usage: ");
+ puts("-c, --command request to get what kind of information from server");
+ puts("-u, --user username");
+ puts("-k, --passwd password");
+ puts("-h, --help show all the arguments");
+ puts("-la, --latitude approximate user position in Latitude");
+ puts("-lo, --longitude approximate user position in Longitude");
+ puts("-al, --altitude approximate user altitude");
+ puts("-p, --pacc approximate accuracy of submitted position");
+ puts("-d, --latency typical latency between the time the server receives the request");
+ puts("example: agps-onlinec -c full -u foo -k passwd -la 25.073270 -lo 121.574805 -al 46.7 -p 999999.00 ");
+}
+
+int establish_conn(void)
+{
+ struct sockaddr_in remote;
+ struct hostent *host;
+ char buf[BUFFER_LEN];
+ int ret, n, i;
+ unsigned char *tmp = NULL;
+
+ /* allocating socket */
+ memset(&remote, 0, sizeof(remote));
+ remote.sin_family = AF_INET;
+ agps.sd = socket(PF_INET, SOCK_STREAM, 0);
+ remote.sin_port = htons(ser_port);
+
+ /* reslove host name to IP addr */
+ host = gethostbyname(ubx_agps_ser);
+
+ if (host != 0)
+ memcpy(&remote.sin_addr, host->h_addr, host->h_length);
+ else{
+ printf("can't reslove host name \n");
+ return 1;
+ }
+
+ connect(agps.sd, (struct sockaddr *)&remote, sizeof(remote));
+
+ memset(buf, 0, BUFFER_LEN);
+ tmp = buf;
+
+ /* send request */
+ for (i = CMD; i < MAX_ITEM; i++){
+ if (agps.request[i] != NULL){
+ strcat(tmp, key[i]);
+ strcat(tmp, agps.request[i]);
+ strcat(tmp, ";");
+ }
+ }
+ strcat(tmp, "\n");
+
+ write(agps.sd, tmp, strlen(tmp));
+
+ /* get response */
+ memset(buf, 0, BUFFER_LEN);
+ tmp = agps_buf;
+
+ while ( (n = read(agps.sd, buf, BUFFER_LEN)) > 0){
+ memcpy(tmp, buf, n);
+ tmp = tmp + n;
+ }
+
+ return 0;
+
+}
+
+int main (int argc, char *argv[])
+{
+ int i, n, aid_length;
+ char **opt_arg;
+ unsigned char *tmp = NULL;
+
+ opt_arg = argv + 1;
+
+ if (!*opt_arg){
+ usage();
+ return 1;
+ }
+
+ /* parse arguments */
+ while (*opt_arg){
+ if (strcmp(*opt_arg, "-c") == 0 || strcmp(*opt_arg, "--command") == 0){
+ if (!*(++opt_arg)){
+ usage();
+ return 1;
+ }
+ agps.request[CMD] = *opt_arg;
+ }
+ else if(strcmp(*opt_arg, "-h") == 0 || strcmp(*opt_arg, "--help") == 0 ){
+ usage();
+ return 1;
+ }
+ else if(strcmp(*opt_arg, "-u") == 0 || strcmp(*opt_arg, "--user") == 0 ){
+ if (!*(++opt_arg)){
+ usage();
+ return 1;
+ }
+ agps.request[USR] = *opt_arg;
+ }
+ else if(strcmp(*opt_arg, "-k") == 0 || strcmp(*opt_arg, "--passwd") == 0 ){
+ if (!*(++opt_arg)){
+ usage();
+ return 1;
+ }
+ agps.request[PASSWD] = *opt_arg;
+ }
+ else if(strcmp(*opt_arg, "-la") == 0 || strcmp(*opt_arg, "--latitude") == 0 ){
+ if (!*(++opt_arg)){
+ usage();
+ return 1;
+ }
+ agps.request[LAT] = *opt_arg;
+ }
+ else if(strcmp(*opt_arg, "-lo") == 0 || strcmp(*opt_arg, "--longitude") == 0 ){
+ if (!*(++opt_arg)){
+ usage();
+ return 1;
+ }
+ agps.request[LON] = *opt_arg;
+ }
+ else if(strcmp(*opt_arg, "-al") == 0 || strcmp(*opt_arg, "--altitude") == 0 ){
+ if (!*(++opt_arg)){
+ usage();
+ return 1;
+ }
+ agps.request[ALT] = *opt_arg;
+ }
+ else if(strcmp(*opt_arg, "-p") == 0 || strcmp(*opt_arg, "--pacc") == 0 ){
+ if (!*(++opt_arg)){
+ usage();
+ return 1;
+ }
+ agps.request[PACC] = *opt_arg;
+ }
+ else if(strcmp(*opt_arg, "-d") == 0 || strcmp(*opt_arg, "--latency") == 0 ){
+ if (!*(++opt_arg)){
+ usage();
+ return 1;
+ }
+ agps.request[LATENCY] = *opt_arg;
+ }
+ ++opt_arg;
+ }
+
+ if (establish_conn())
+ goto parsing_fail;
+ else
+ printf("connect to %s \n", ubx_agps_ser);
+
+ /* content length */
+ tmp = strstr(agps_buf, CONTENT_LENGTH);
+
+ if (!tmp){
+ printf("content_length is missing, msg might be broken \n");
+ goto parsing_fail;
+ }
+
+ tmp = tmp + strlen(CONTENT_LENGTH);
+ printf("aid data %d bytes received\n", atoi(tmp));
+
+ aid_length = atoi(tmp);
+
+ /* content type */
+ if (tmp = strstr(agps_buf, CONTENT_TYPE)){
+ if (tmp = strstr(agps_buf, UBX_FORMAT))
+ /* skip 2 empty line */
+ tmp = tmp + strlen(UBX_FORMAT) + 4;
+ }
+ else{
+ printf("content_type is missing, msg might be broken \n");
+ goto parsing_fail;
+ }
+
+ /* open device file */
+ dev_open();
+
+ /* resetting GPS */
+ gps_reset();
+
+ if (ubx_raw_write(tmp, aid_length))
+ printf("aid data raw written failed\n");
+ else
+ printf("aid data raw written ok\n");
+
+ close(agps.sd);
+ close(agps.fd);
+
+ return 0;
+
+parsing_fail:
+ close(agps.sd);
+ return -1;
+}
Added: developers/matt_hsu/agps-online/agps-onlinec.h
===================================================================
--- developers/matt_hsu/agps-online/agps-onlinec.h (rev 0)
+++ developers/matt_hsu/agps-online/agps-onlinec.h 2008-06-06 11:37:46 UTC (rev 4479)
@@ -0,0 +1,26 @@
+/*
+ * Header file of agps-onlinec.c
+ *
+ * CopyRight (c) 2008 by OpenMoko, Inc.
+ * Matt Hsu <matt_hsu at openmoko.org>
+ */
+
+enum {
+ CMD,
+ USR,
+ PASSWD,
+ LAT,
+ LON,
+ ALT,
+ PACC,
+ LATENCY,
+ MAX_ITEM
+};
+
+struct agps_onlinec_data{
+
+ int fd; /* UART device file */
+ int sd; /* network socket */
+
+ char *request[MAX_ITEM]; /* ptr array of key parameter */
+};
Added: developers/matt_hsu/agps-online/ubx.c
===================================================================
--- developers/matt_hsu/agps-online/ubx.c (rev 0)
+++ developers/matt_hsu/agps-online/ubx.c 2008-06-06 11:37:46 UTC (rev 4479)
@@ -0,0 +1,140 @@
+/*
+ * This program is based on gllin.c of project openmoko-agps with slight modification.
+ *
+ * You can find this project here:
+ * http://projects.openmoko.org/projects/openmoko-agpsui/ *
+ */
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <termios.h>
+
+#include "ubx.h"
+#include "agps-onlinec.h"
+
+#define UBX_DEBUG 0
+
+extern struct agps_onlinec_data agps;
+
+int dev_open(void)
+{
+ struct termios tio;
+
+ agps.fd = open(pipeName, O_RDWR);
+
+ if (tcgetattr(agps.fd, &tio) < 0){
+ printf("can't get attribute \n");
+ return 1;
+ }
+
+ tio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
+ tio.c_lflag &= ~ICANON;
+ tio.c_lflag &= ~ECHO;
+
+ tcflush(agps.fd, TCIFLUSH);
+ tcsetattr(agps.fd,TCSANOW,&tio);
+
+ return 0;
+}
+
+static ssize_t bwrite(const unsigned char *buf, size_t count, int delay)
+{
+ size_t i;
+
+ if (delay)
+ {
+ for (i = 0; i < count; i++)
+ {
+ if (write(agps.fd, buf + i, 1) != 1)
+ return -1;
+ usleep(delay * 1000);
+ }
+ }
+ else
+ return write(agps.fd, (const unsigned char *) buf, count);
+
+ return i;
+}
+
+int ubx_raw_write(const unsigned char *raw_data, int size)
+{
+ if (bwrite(raw_data, size, 0) != size){
+ printf("ubx failed \n");
+ return 1;
+ }
+
+ return 0;
+}
+
+static void ubx_send_msg(char cls, char id, const unsigned char *ubx, int size, int delay)
+{
+ unsigned char header[6], checksum[2];
+ int i;
+
+ header[0] = 0xb5;
+ header[1] = 0x62;
+ header[2] = cls;
+ header[3] = id;
+ header[4] = size & 0xff;
+ header[5] = (size >> 8) & 0xff;
+
+ checksum[0] = checksum[1] = 0;
+ for (i = 2; i < 6; i++)
+ {
+ checksum[0] += header[i];
+ checksum[1] += checksum[0];
+ }
+
+ for (i = 0; i < size; i++)
+ {
+ checksum[0] += ubx[i];
+ checksum[1] += checksum[0];
+ }
+
+ if (bwrite(header, 6, delay) != 6)
+ goto fail;
+
+ if (size)
+ {
+ if (bwrite((void *) ubx, size, delay) != size)
+ goto fail;
+ }
+
+ if (bwrite(checksum, 2, delay) != 2)
+ goto fail;
+
+ if (UBX_DEBUG)
+ {
+ printf("ubx sent: ");
+ for (i = 0; i < 6; i++)
+ printf("%02x ", header[i]);
+ if (size)
+ {
+ for (i = 0; i < size; i++)
+ printf("%02x ", ubx[i]);
+ }
+
+ printf("%02x %02x\n", checksum[0], checksum[1]);
+ }
+
+ return;
+fail:
+ printf("ubx failed\n");
+}
+
+void gps_reset(void)
+{
+ unsigned char ubx[4];
+ int bbr = 0x7ff;
+
+ ubx[0] = bbr & 0xff;
+ ubx[1] = (bbr >> 8) & 0xff;
+ ubx[2] = 0x2; /* Resetting GPS */
+ ubx[3] = 0x0;
+
+ ubx_send_msg(0x06, 0x04, ubx, 4, 4);
+ sleep(2);
+}
+
Added: developers/matt_hsu/agps-online/ubx.h
===================================================================
--- developers/matt_hsu/agps-online/ubx.h (rev 0)
+++ developers/matt_hsu/agps-online/ubx.h 2008-06-06 11:37:46 UTC (rev 4479)
@@ -0,0 +1,17 @@
+/*
+ * Header file of ubx.c
+ *
+ * This program is based on gllin.c of project openmoko-agps with slight modification.
+ * You can find this project here:
+ * http://projects.openmoko.org/projects/openmoko-agpsui/
+ *
+ */
+
+#define pipeName "/dev/ttySAC1"
+//#define pipeName "/dev/ttyS0"
+
+int dev_open(void);
+
+int ubx_raw_write(const unsigned char *raw_data, int size);
+
+void gps_reset(void);
More information about the commitlog
mailing list