r4135 - in developers/sean_chiang: . gsm-pm

sean_chiang at sita.openmoko.org sean_chiang at sita.openmoko.org
Fri Feb 29 05:12:56 CET 2008


Author: sean_chiang
Date: 2008-02-29 05:12:52 +0100 (Fri, 29 Feb 2008)
New Revision: 4135

Added:
   developers/sean_chiang/gsm-pm/
   developers/sean_chiang/gsm-pm/Makefile
   developers/sean_chiang/gsm-pm/gsm-pm.c
Log:
A wrapper for power control of GSM modem

Added: developers/sean_chiang/gsm-pm/Makefile
===================================================================
--- developers/sean_chiang/gsm-pm/Makefile	2008-02-29 03:14:45 UTC (rev 4134)
+++ developers/sean_chiang/gsm-pm/Makefile	2008-02-29 04:12:52 UTC (rev 4135)
@@ -0,0 +1,5 @@
+CC=arm-angstrom-linux-gnueabi-gcc
+all: 
+	$(CC) -o gsm-pm gsm-pm.c
+clean:
+	rm gsm-pm

Added: developers/sean_chiang/gsm-pm/gsm-pm.c
===================================================================
--- developers/sean_chiang/gsm-pm/gsm-pm.c	2008-02-29 03:14:45 UTC (rev 4134)
+++ developers/sean_chiang/gsm-pm/gsm-pm.c	2008-02-29 04:12:52 UTC (rev 4135)
@@ -0,0 +1,169 @@
+/* gsm-pm                                                                                                                                             
+ *
+ * (C) 2006-2007 by OpenMoko, Inc.
+ * Written by Sean Chiang <sean_chiang at openmoko.org>
+ * All Rights Reserved
+ *
+ * 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 <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <termios.h>
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
+#endif
+
+struct bdrt { 
+	int bps;
+	u_int32_t b;
+};     
+
+static struct bdrt bdrts[] = {
+	{ 0, B0 },
+	{ 9600, B9600 },
+	{ 19200, B19200 },
+	{ 38400, B38400 },
+	{ 57600, B57600 },
+	{ 115200, B115200 },
+	{ 230400, B230400 },
+	{ 460800, B460800 },
+	{ 921600, B921600 },
+};     
+
+static int set_baudrate(int fd, int baudrate, int hwflow) 
+{
+	int i;
+	u_int32_t bd = 0;
+	struct termios ti;
+
+	for (i = 0; i < ARRAY_SIZE(bdrts); i++) {
+		if (bdrts[i].bps == baudrate)
+			bd = bdrts[i].b;
+	}
+	if (bd == 0) {
+		printf("can't set Baud rate\n");	
+		exit(1);
+	}	
+
+	i = tcgetattr(fd, &ti);
+	if (i < 0) {
+		printf("can't get attr\n");	
+		exit(1);
+	}
+
+	i = cfsetispeed(&ti, B0);
+	if (i < 0) {
+		printf("can't set input baud rate to B0\n");	
+		exit(1);
+	}
+
+	i = cfsetospeed(&ti, bd);
+	if (i < 0) {
+		printf("can't set output baud rate");	
+		exit(1);
+	}
+
+	if (hwflow)
+		ti.c_cflag |= CRTSCTS;
+	else
+		ti.c_cflag &= ~CRTSCTS;
+
+	return tcsetattr(fd, 0, &ti);
+}	
+
+int main(int argc, char **argv) {
+
+	char *p_DL, *p_POW, *p_RES, *p_OPTS, *p_DEV;
+	char buf[128];
+	pid_t child;
+	int fd;
+
+	p_DL = getenv("GSM_DL");
+	p_POW = getenv("GSM_POW");
+	p_RES = getenv("GSM_RES");
+	p_OPTS = getenv("GSMD_OPTS");
+	p_DEV = getenv("GSM_DEV");
+
+	if ( p_DL ) {
+		sprintf(buf, "echo \"1\" > %s", p_DL);
+		system(buf);
+		sleep(1);
+	}
+
+	if ( p_POW ) {
+		sprintf(buf, "echo \"0\" > %s", p_POW);
+		system(buf);
+		sleep(1);
+		sprintf(buf, "echo \"1\" > %s", p_POW);
+		system(buf);
+		sleep(1);
+	}
+
+	if ( p_RES ) {
+		sprintf(buf, "echo \"1\" > %s", p_RES);
+		system(buf);
+		sleep(1);
+		sprintf(buf, "echo \"0\" > %s", p_RES);
+		system(buf);
+		sleep(2);
+	}
+
+	printf("Starting GSM daemon: \n");
+	
+	if ( p_OPTS && p_DEV ) {
+		
+		sprintf(buf, "start-stop-daemon -S -x /usr/sbin/gsmd -- gsmd -p %s %s >/tmp/gsm.log 2>&1", p_DEV,p_OPTS);
+		system(buf);
+	}
+	
+	{
+		wait();
+		printf("gsmd is killed\n");
+
+		// Send AT at POFF
+		fd = open(p_DEV, O_RDWR);
+		if (fd < 0) {
+			printf("can't open device %s", p_DEV);
+			exit(1);
+		}  
+
+		if (set_baudrate(fd, 115200, 1) < 0) { 
+			printf("can't set baudrate\n");
+			exit(1);
+		}
+		
+		write(fd, "AT", 2);	
+		write(fd, " \r", 2);	
+		write(fd,"AT at POFF", 7);
+		close(fd);
+		
+		sleep(1);
+
+		if ( p_POW ) {
+			sprintf(buf, "echo \"0\" > %s", p_POW);
+			system(buf);
+		}
+	}
+
+	exit(0);
+}





More information about the commitlog mailing list