aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/obsolete/initscripts/initscripts-openmoko/campgsm
blob: 5be67086c284de24e6ba1b667d2b59d6422c0a00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh

VERBOSE=no

PIDS=`pidof -x "$0"`

# make sure pidof sees us
if [ -z "$PIDS" ]; then
	exec "$0"
	exit 1
fi

GSM_DEVICE="/dev/ttySAC0"
GSM_POWER="/sys/devices/platform/neo1973-pm-gsm.0/power_on"
GSM_RESET="/sys/devices/platform/neo1973-pm-gsm.0/reset"

GSM_COMMANDS="Z E0V1 +CFUN=1 +COPS=0"

gsm_running () {
	if [ "$PIDS" != "$$" ]; then
		return 0
	else
		return 1
	fi
}

gsm_send () {
	cmd="$1"
	echo -ne "AT${cmd}\r" > "$GSM_DEVICE"
}

# this function might block forever
gsm_wait () {
	ret="UNKNOWN"

	while read status; do
		case "$status" in
			OK*)
				ret="OK"
				;;
			ERROR*)
				ret="ERROR"
				;;
			*)
				ret="UNKNOWN"
				;;
		esac
		if [ "x$ret" != "xUNKNOWN" ]; then
			break
		fi
	done < "$GSM_DEVICE"

	if [ "x$ret" != "xOK" ]; then
		return 1
	fi

	return 0
}

gsm_setup () {
	[ $VERBOSE == "yes" ] && echo -n "Powering up GSM modem..."
	echo 0 > "$GSM_POWER"; sleep 1
	echo 1 > "$GSM_POWER"; sleep 1
	echo 1 > "$GSM_RESET"; sleep 1
	echo 0 > "$GSM_RESET"; sleep 4
	[ $VERBOSE == "yes" ] && echo "done"

	stty -F "$GSM_DEVICE" cooked -opost -echo crtscts 115200
}

gsm_wakeup () {
	[ $VERBOSE == "yes" ] && echo "Waking up GSM modem"

	# there is at most one OK/ERROR even if we send multiple commands
	gsm_send ""
	gsm_send ""
	gsm_send ""

	if gsm_wait; then
		[ $VERBOSE == "yes" ] && echo "GSM modem awake"
		return 0
	else
		echo "failed to wake up GSM modem"
		return 1
	fi
}

start () {
	gsm_setup

	if gsm_wakeup; then
		for cmd in $GSM_COMMANDS
		do
			[ $VERBOSE == "yes" ] && echo "Sending AT$cmd"
			gsm_send "$cmd"

			if ! gsm_wait; then
				echo "AT$cmd failed"
				break
			fi
		done
	fi
}

set -e

case "$1" in
	start)
		if gsm_running; then
			echo "Another instance is already running"
			exit 1
		fi

		start &
		;;
	stop)
		if gsm_running; then
			PIDS=`pidof -x -o "$$" "$0"`
			echo -n "Stopping other instances..."
			kill $PIDS
			echo "done"
		fi
		;;
	*)
		echo "Usage: $0 {start|stop}"
		exit 1
		;;
esac