Exemplo n.º 1
0
func (sm *StateMachine) Connect() error {
	if e := C.GSM_InitConnection(sm.g, 1); e != C.ERR_NONE {
		return Error{"InitConnection", e}
	}
	C.setStatusCallback(sm.g, &sm.status)
	sm.smsc.Location = 1
	if e := C.GSM_GetSMSC(sm.g, &sm.smsc); e != C.ERR_NONE {
		return Error{"GetSMSC", e}
	}
	return nil
}
Exemplo n.º 2
0
// Sends message
func (g *GSM) SendSMS(text, number string) (err error) {
	var sms C.GSM_SMSMessage
	var smsc C.GSM_SMSC

	sms.PDU = C.SMS_Submit                           // submit message
	sms.UDH.Type = C.UDH_NoUDH                       // no UDH, just a plain message
	sms.Coding = C.SMS_Coding_Default_No_Compression // default coding for text
	sms.Class = 1                                    // class 1 message (normal)

	C.EncodeUnicode((*C.uchar)(unsafe.Pointer(&sms.Text)), C.CString(text), C.int(len(text)))
	C.EncodeUnicode((*C.uchar)(unsafe.Pointer(&sms.Number)), C.CString(number), C.int(len(number)))

	// we need to know SMSC number
	smsc.Location = 1
	e := C.GSM_GetSMSC(g.sm, &smsc)
	if e != ERR_NONE {
		err = errors.New(errorString(int(e)))
		return
	}

	// set SMSC number in message
	sms.SMSC.Number = smsc.Number

	// Set flag before callind SendSMS, some phones might give instant response
	smsSendStatus = ERR_TIMEOUT

	// send message
	e = C.GSM_SendSMS(g.sm, &sms)
	if e != ERR_NONE {
		err = errors.New(errorString(int(e)))
		return
	}

	// wait for network reply
	for {
		C.GSM_ReadDevice(g.sm, C.gboolean(1))
		if smsSendStatus == ERR_NONE {
			break
		}
		if smsSendStatus != ERR_TIMEOUT {
			err = errors.New(errorString(int(smsSendStatus)))
			break
		}
	}

	return
}