Beispiel #1
0
// Creates new state maschine using cf configuration file or default
// configuration file if cf == "".
func NewStateMachine(cf string) (*StateMachine, error) {
	//C.setDebug()
	var config *C.INI_Section
	if cf != "" {
		cs := C.CString(cf)
		defer C.free(unsafe.Pointer(cs))
		if e := C.GSM_FindGammuRC(&config, cs); e != C.ERR_NONE {
			return nil, Error{"FindGammuRC", e}
		}
	} else {
		if e := C.GSM_FindGammuRC(&config, nil); e != C.ERR_NONE {
			return nil, Error{"FindGammuRC", e}
		}
	}
	defer C.INI_Free(config)

	sm := new(StateMachine)
	sm.g = C.GSM_AllocStateMachine()
	if sm.g == nil {
		panic("out of memory")
	}

	if e := C.GSM_ReadConfig(config, C.GSM_GetConfig(sm.g, 0), 0); e != C.ERR_NONE {
		sm.free()
		return nil, Error{"ReadConfig", e}
	}
	C.GSM_SetConfigNum(sm.g, 1)
	sm.Timeout = 15 * time.Second

	runtime.SetFinalizer(sm, (*StateMachine).free)
	return sm, nil
}
Beispiel #2
0
// Reads configuration file
func (g *GSM) SetConfig(config string) (err error) {
	path := C.CString(config)
	defer C.free(unsafe.Pointer(path))

	var cfg *C.INI_Section
	defer C.INI_Free(cfg)

	// find configuration file
	e := C.GSM_FindGammuRC(&cfg, path)
	if e != ERR_NONE {
		err = errors.New(errorString(int(e)))
		return
	}

	// read it
	e = C.GSM_ReadConfig(cfg, C.GSM_GetConfig(g.sm, 0), 0)
	if e != ERR_NONE {
		err = errors.New(errorString(int(e)))
		return
	}

	// we have one valid configuration
	C.GSM_SetConfigNum(g.sm, 1)
	return
}