Adding config file for sharing.
[iot2.git] / benchmarks / other / DLinkAlarm / alarm.sh
1 #!/bin/bash
2 # modify next two line for your DSP-W215
3 IP="192.168.1.183"
4 PIN="215530"
5 # do not modify after this line if you don't know what you are doing
6
7 function usage {
8         echo -e "\nUsage: $(basename $0) [OPTION]"
9         echo -e "\nOPTION:"
10         echo -e "\t--getstate"
11         echo -e "\t--stop"
12   echo -e "\t--noise SoundType(1-6) Volume(0-100) Duration(??)"
13 }
14
15 function hash_hmac {
16   data="$1"
17   key="$2"
18   echo -n "$data" | openssl dgst "-md5" -hmac "$key" -binary | xxd -ps -u
19 }
20
21 contentType="Content-Type: text/xml; charset=utf-8"
22 soapLogin="SOAPAction: \"http://purenetworks.com/HNAP1/Login\""
23
24 #Get Login data
25
26 head="<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>"
27 end="</soap:Body></soap:Envelope>"
28
29 message="<Login xmlns=\"http://purenetworks.com/HNAP1/\"><Action>request</Action><Username>admin</Username><LoginPassword></LoginPassword><Captcha/></Login>"
30 loginrequest="$head$message$end"
31 #echo "Login request: $loginrequest"
32
33 #ret=`curl -s -X POST -H "$contentType" -H "$soapLogin" --data-binary "$loginrequest" http://$IP/HNAP1`
34 ret=`curl -v -X POST -H "$contentType" -H "$soapLogin" --data-binary "$loginrequest" http://$IP/HNAP1`
35 echo -e "Returned value: $ret"
36
37 function getResult {
38   opt=`echo -n "$ret" | grep -Po "(?<=<$1>).*(?=</$1>)"`
39   echo -n "$opt"
40 }
41
42 #echo -e "Challenge: "
43 #echo -e `getResult Challenge`
44
45 challenge=`getResult Challenge`
46 cookie="Cookie: uid=`getResult Cookie`"
47 publickey="`getResult PublicKey`$PIN"
48 privatekey=`hash_hmac "$challenge" "$publickey"`
49 password=`hash_hmac "$challenge" "$privatekey"`
50 timestamp=`date +%s`
51 auth_str="$timestamp\"http://purenetworks.com/HNAP1/Login\""
52 auth=`hash_hmac "$auth_str" "$privatekey"`
53 hnap_auth="HNAP_AUTH: $auth $timestamp"
54
55 message="<Login xmlns=\"http://purenetworks.com/HNAP1/\"><Action>login</Action><Username>admin</Username><LoginPassword>$password</LoginPassword><Captcha/></Login>"
56
57 login="$head$message$end"
58 echo "Login info: $login"
59
60 #Get Login Result
61 #ret=`curl -s -X POST -H "$contentType" -H "$soapLogin" -H "$hnap_auth" -H "$cookie" --data-binary "$login" http://$IP/HNAP1`
62 ret=`curl -v -X POST -H "$contentType" -H "$soapLogin" -H "$hnap_auth" -H "$cookie" --data-binary "$login" http://$IP/HNAP1`
63 echo -e "Returned value: $ret"
64
65 echo -e "\nUsage: $(basename $0) [OPTION]"
66
67 case "$1" in
68 --getstate )
69         #Next 2 rows to modify query
70         method="GetSirenAlarmSettings"
71         message="<$method xmlns=\"http://purenetworks.com/HNAP1/\"><ModuleID>1</ModuleID>><Controller>1</Controller></$method>"
72
73         #Do not modify after this line
74         soapAction="SOAPAction: \"http://purenetworks.com/HNAP1/$method\""
75         authStr="$timestamp\"http://purenetworks.com/HNAP1/$method\""
76         auth=`hash_hmac "$authStr" "$privatekey"`
77         hnap_auth="HNAP_AUTH: $auth $timestamp"
78         data="$head$message$end"
79
80         #Get Device state from GetSocketSettings
81         #ret=`curl -s -X POST -H "$contentType" -H "$soapAction" -H "$hnap_auth" -H "$cookie" --data-binary "$data" http://$IP/HNAP1`
82         ret=`curl -v -X POST -H "$contentType" -H "$soapAction" -H "$hnap_auth" -H "$cookie" --data-binary "$data" http://$IP/HNAP1`
83         echo -e "Timestamp=$timestamp\tSOAPAction=$soapAction\tAuthStr=$authStr\tAUTH=$auth\tHNAP=$hnap_auth\tRET = $ret" #This line is for debug purpose
84         ;;
85 --noise )
86     #Next 2 rows to modify query
87     method="SetSoundPlay"
88     message="<$method xmlns=\"http://purenetworks.com/HNAP1/\"><ModuleID>1</ModuleID><Controller>1</Controller><SoundType>$2</SoundType><Volume>$3</Volume><Duration>$4</Duration></$method>"
89
90     #Do not modify after this line
91     soapAction="SOAPAction: \"http://purenetworks.com/HNAP1/$method\""
92     authStr="$timestamp\"http://purenetworks.com/HNAP1/$method\""
93     auth=`hash_hmac "$authStr" "$privatekey"`
94     hnap_auth="HNAP_AUTH: $auth $timestamp"
95     data="$head$message$end"
96     echo -e "Sound data: $data"
97     
98     #Get Device state from GetSocketSettings
99     #ret=`curl -s -X POST -H "$contentType" -H "$soapAction" -H "$hnap_auth" -H "$cookie" --data-binary "$data" http://$IP/HNAP1`
100     ret=`curl -v -X POST -H "$contentType" -H "$soapAction" -H "$hnap_auth" -H "$cookie" --data-binary "$data" http://$IP/HNAP1`
101     echo -e "Return value: $ret"
102           ;;
103 --stop )
104           #Next 2 rows to modify query
105           method="SetAlarmDismissed"
106           message="<$method xmlns=\"http://purenetworks.com/HNAP1/\"><ModuleID>1</ModuleID><Controller>1</Controller></$method>"
107     
108     #Do not modify after this line
109           soapAction="SOAPAction: \"http://purenetworks.com/HNAP1/$method\""
110     authStr="$timestamp\"http://purenetworks.com/HNAP1/$method\""
111     auth=`hash_hmac "$authStr" "$privatekey"`
112     hnap_auth="HNAP_AUTH: $auth $timestamp"
113     data="$head$message$end"
114     echo -e "Stop data: $data"
115     
116     #Get Device state from GetSocketSettings
117     #ret=`curl -s -X POST -H "$contentType" -H "$soapAction" -H "$hnap_auth" -H "$cookie" --data-binary "$data" http://$IP/HNAP1`
118     ret=`curl -v -X POST -H "$contentType" -H "$soapAction" -H "$hnap_auth" -H "$cookie" --data-binary "$data" http://$IP/HNAP1`
119           ;;
120 * )
121           usage
122           ;;
123 esac