Changeset 4457
- Timestamp:
- 05/28/08 01:27:42 (5 years ago)
- Location:
- developers/alphaone/u-blox/libubx
- Files:
-
- 4 edited
-
ChangeLog (modified) (1 diff)
-
include/libubx.h (modified) (1 diff)
-
src/libubx.c (modified) (7 diffs)
-
src/ubx-get.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
developers/alphaone/u-blox/libubx/ChangeLog
r4456 r4457 1 2008-05-20 Daniel Willmann <daniel@openmoko.org> 2 3 * libubx/include/libubx.h, libubx/src/libubx.c: Add funtions for parsing and 4 generating almanac and ephemeris packets. 5 * libubx/src/ubx-get.c: Add support to get almanac from GPS device. 6 1 7 2008-05-12 Daniel Willmann <daniel@openmoko.org> 2 8 -
developers/alphaone/u-blox/libubx/include/libubx.h
r4409 r4457 92 92 uint8_t checksum(ubx_hdr *header, uint8_t *msg); 93 93 94 uint16_t ubx_msg_encode(ubx_ hdr *header, uint8_t*msg, uint8_t **data);94 uint16_t ubx_msg_encode(ubx_msg *msg, uint8_t **data); 95 95 ubx_msg *ubx_msg_decode(uint16_t length, uint8_t *data); 96 96 -
developers/alphaone/u-blox/libubx/src/libubx.c
r4455 r4457 29 29 uint8_t checksum(ubx_hdr *header, uint8_t *msg) 30 30 { 31 uint8_t ck_a = 0, ck_b = 0 ;31 uint8_t ck_a = 0, ck_b = 0, i; 32 32 uint16_t *len = &header->length; 33 33 … … 40 40 ck_a += ((uint8_t *)len)[1]; 41 41 ck_b += ck_a; 42 43 for (i=0; i < header->length; i++) { 44 ck_a += msg[i]; 45 ck_b += ck_a; 46 } 42 47 43 48 if ((ck_a == header->ck_a)&&(ck_b == header->ck_b)) { … … 51 56 } 52 57 53 uint16_t ubx_msg_encode(ubx_hdr *header, uint8_t *msg, uint8_t **data) 54 { 58 void ubx_aid_encode(uint8_t *dst, ubx_msg *src) 59 { 60 ubx_hdr *hdr = src->header; 61 switch (hdr->id) { 62 case UBX_ID_ALM: 63 /* UBX AID_ALM */ 64 if (hdr->length == 0 || hdr->length == 1 || 65 hdr->length == 8 || hdr->length == 40) { 66 memcpy(dst, src->payload, hdr->length); 67 } else { 68 printf("Malformed UBX AID_ALM packet\n"); 69 } 70 return; 71 break; 72 case UBX_ID_EPH: 73 /* UBX_AID_EPH */ 74 if (hdr->length == 0 || hdr->length == 1 || 75 hdr->length == 8 || hdr->length == 104) { 76 memcpy(dst, src->payload, hdr->length); 77 } else { 78 printf("Malformed UBX AID_EPH packet\n"); 79 } 80 return; 81 break; 82 default: 83 printf("Unknown UBX AID Id 0x%x\n", hdr->id); 84 } 85 } 86 87 void ubx_msg_payload_encode(uint8_t *dst, ubx_msg *src) 88 { 89 ubx_hdr *hdr = src->header; 90 switch (hdr->cls) { 91 case UBX_CL_AID: 92 ubx_aid_encode(dst, src); 93 break; 94 case UBX_CL_CFG: 95 // ubx_cfg_encode(dst, src); 96 default: 97 printf("Unknown UBX class 0x%x\n", hdr->cls); 98 } 99 } 100 101 102 uint16_t ubx_msg_encode(ubx_msg *msg, uint8_t **data) 103 { 104 ubx_hdr *header = msg->header; 55 105 uint8_t *temp; 106 56 107 *data = malloc(header->length + 8); 57 108 if (!*data) … … 64 115 // Copy the header 65 116 // XXX: Handle Endianess 66 memcpy(temp, header, 4);117 memcpy(temp, msg->header, 4); 67 118 temp += 4; 68 119 // And the message 69 memcpy(temp, msg, header->length);120 ubx_msg_payload_encode(temp, msg); 70 121 temp += header->length; 71 122 *temp++ = header->ck_a; 72 123 *temp++ = header->ck_b; 73 124 return header->length + 8; 125 } 126 127 uint8_t *ubx_aid_decode(uint8_t id, uint16_t length, uint8_t *data) 128 { 129 uint8_t *value; 130 131 switch (id) { 132 case UBX_ID_ALM: 133 /* AID_ALM */ 134 if (length == 0 || length == 1 || 135 length == 8 || length == 40) { 136 value = malloc(sizeof(ubx_msg_aid_alm)); 137 memcpy(value, data, length); 138 } 139 break; 140 case UBX_ID_EPH: 141 /* AID_EPH*/ 142 if (length == 0 || length == 1 || 143 length == 8 || length == 104) { 144 value = malloc(sizeof(ubx_msg_aid_eph)); 145 memcpy(value, data, length); 146 } 147 break; 148 default: 149 printf("Unknown AID ID 0x%x\n", id); 150 return NULL; 151 } 152 return value; 153 } 154 155 uint8_t *ubx_msg_payload_decode(uint8_t cls, uint8_t id, uint16_t length, uint8_t *data) 156 { 157 switch (cls) { 158 case UBX_CL_AID: 159 return ubx_aid_decode(id, length, data); 160 break; 161 default: 162 printf("Unknown class 0x%x\n", cls); 163 } 164 return 0; 74 165 } 75 166 … … 90 181 data += 2; 91 182 92 if (length < header->length + 8) 93 return 0; 183 if (length < header->length + 8) { 184 free(header); 185 return 0; 186 } 94 187 95 188 header->ck_a = data[header->length]; … … 97 190 98 191 if (!checksum(header, data)) { 99 free (header);192 free(header); 100 193 return 0; 101 194 } … … 105 198 msg->header = header; 106 199 107 msg->payload = ubx_ parse_msg(header->cls, header->id, header->length, data);200 msg->payload = ubx_msg_payload_decode(header->cls, header->id, header->length, data); 108 201 109 202 return msg; -
developers/alphaone/u-blox/libubx/src/ubx-get.c
r4456 r4457 20 20 */ 21 21 22 #include <sys/types.h> 23 #include <sys/stat.h> 24 #include <errno.h> 22 25 #include <getopt.h> 23 26 #include <stdlib.h> 24 27 #include <stdio.h> 28 #include <string.h> 29 #include <termios.h> 30 #include <unistd.h> 31 #include <fcntl.h> 25 32 #include <libubx.h> 26 33 27 34 28 35 /* Flag set by `--verbose'. */ 29 static int verbose _flag;36 static int verbose; 30 37 static int get_alm, get_eph; 31 static char *gpsname, *filename; 38 static char *gpsname, *filename = NULL; 39 40 static int gpsopen(const char *path, speed_t speed); 41 42 void usage () 43 { 44 printf("Usage:\n"); 45 printf("\t--verbose Display more information about what's going on\n"); 46 printf("\t--gps <name> Specify GPS device path\n"); 47 printf("\t--file <name> Specify file name to output response\n"); 48 printf("\t--almanac Request almanac from GPS\n"); 49 printf("\t--ephemeris Request ephemeris from GPS\n"); 50 printf("\t--help Show this help\n"); 51 exit(1); 52 } 53 54 void request_almanac(int fd) 55 { 56 uint8_t *data, len, i; 57 ubx_msg msg; 58 msg.header = malloc(sizeof(ubx_hdr)); 59 msg.header->cls = UBX_CL_AID; 60 msg.header->id = UBX_ID_ALM; 61 msg.header->length = 0; 62 msg.payload = NULL; 63 64 len = ubx_msg_encode(&msg, &data); 65 66 if (verbose) { 67 printf("Writing almanac request: "); 68 for (i=0;i<len; i++) { 69 printf("0x%x ", data[i]); 70 } 71 printf("\n"); 72 } 73 74 write(fd, data, len); 75 } 32 76 33 77 int main (int argc, char *argv[]) 34 78 { 35 int c;79 int c, gpsfd; 36 80 37 while (1) 38 { 39 static struct option long_options[] = 40 { 41 {"verbose", no_argument, 0, 'v'}, 42 {"gps", required_argument, 0, 'g'}, 43 {"file", required_argument, 0, 'f'}, 44 {"almanac", no_argument, 0, 'a'}, 45 {"ephemeris", no_argument, 0, 'e'}, 46 {0, 0, 0, 0} 47 }; 48 /* getopt_long stores the option index here. */ 49 int option_index = 0; 81 while (1) 82 { 83 static struct option long_options[] = 84 { 85 {"verbose", no_argument, 0, 'v'}, 86 {"gps", required_argument, 0, 'g'}, 87 {"file", required_argument, 0, 'f'}, 88 {"almanac", no_argument, 0, 'a'}, 89 {"ephemeris", no_argument, 0, 'e'}, 90 {"help", no_argument, 0, 'h'}, 91 {0, 0, 0, 0} 92 }; 50 93 51 c = getopt_long (argc, argv, "vg:f:ae", 52 long_options, &option_index);94 /* getopt_long stores the option index here. */ 95 int option_index = 0; 53 96 54 /* Detect the end of the options. */ 55 if (c == -1) 56 break; 97 c = getopt_long (argc, argv, "vg:f:aeh", 98 long_options, &option_index); 57 99 58 switch (c) 59 { 60 case 0: 61 /* If this option set a flag, do nothing else now. */ 62 if (long_options[option_index].flag != 0) 63 break; 64 printf ("option %s", long_options[option_index].name); 65 if (optarg) 66 printf (" with arg %s", optarg); 67 printf ("\n"); 68 break; 100 /* Detect the end of the options. */ 101 if (c == -1) 102 break; 69 103 70 case 'v': 71 verbose_flag = 1; 72 break; 104 switch (c) 105 { 106 case 'v': 107 verbose = 1; 108 break; 73 109 74 case 'g':75 gpsname = optarg;76 break;110 case 'g': 111 gpsname = optarg; 112 break; 77 113 78 case 'f':79 filename = optarg;80 break;114 case 'f': 115 filename = optarg; 116 break; 81 117 82 case 'a':83 get_alm = 1;84 break;118 case 'a': 119 get_alm = 1; 120 break; 85 121 86 case 'e':87 get_eph = 1;88 break;122 case 'e': 123 get_eph = 1; 124 break; 89 125 90 case '?':91 /* getopt_long already printed an error message. */ 92 break;126 case 'h': 127 usage(); 128 break; 93 129 94 default:95 abort();96 }97 }130 default: 131 usage(); 132 } 133 } 98 134 99 /* Print any remaining command line arguments (not options). */ 100 if (optind < argc) 101 { 102 printf ("non-option ARGV-elements: "); 103 while (optind < argc) 104 printf ("%s ", argv[optind++]); 105 putchar ('\n'); 106 } 135 if (optind < argc) { 136 usage(); 137 } 107 138 108 exit (0); 139 if (gpsname == NULL) { 140 printf("Please specify a GPS device\n"); 141 usage(); 142 } 143 144 gpsfd = gpsopen(gpsname, 9600); 145 146 if (gpsfd < 0) { 147 printf("GPS device could not be opened at %s\n", gpsname); 148 printf("Reason: %s\n", strerror(errno)); 149 exit(1); 150 } 151 gpsfd=1; 152 153 if (get_alm) { 154 request_almanac(gpsfd); 155 } 156 109 157 } 110 158 159 static int gpsopen(const char *path, speed_t speed) 160 { 161 int fd; 162 struct termios term; 163 164 fd = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK); 165 if (fd < 0) 166 return fd; 167 168 if (speed == B0) 169 return fd; 170 171 if (tcgetattr(fd, &term) < 0) { 172 // close(fd); 173 printf("tcgetattr failed!\n"); 174 return fd; 175 } 176 177 term.c_cflag &= ~(PARENB | PARODD | CRTSCTS); 178 term.c_cflag |= CREAD | CLOCAL; 179 180 term.c_iflag &= ~(PARMRK | INPCK); 181 term.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD); 182 term.c_cflag |= CS8; 183 184 term.c_lflag &= ~ICANON; 185 term.c_lflag &= ~ECHO; 186 187 cfsetspeed(&term, speed); 188 189 if (tcsetattr(fd, TCSANOW, &term) < 0) { 190 printf("tcsetattr failed\n"); 191 close(fd); 192 193 return -1; 194 } 195 196 return fd; 197 }
Note: See TracChangeset
for help on using the changeset viewer.
