/** * Internal function which just closes connection and cleans up structures. */ GSM_Error GSM_CloseConnection(GSM_StateMachine *s) { GSM_Error error; smprintf(s, "[Closing]\n"); /* Terminate protocol */ error = s->Protocol.Functions->Terminate(s); if (error != ERR_NONE) return error; /* Close the device */ error = s->Device.Functions->CloseDevice(s); if (error != ERR_NONE) return error; /* Release lock if there was any */ if (s->LockFile != NULL) { unlock_device(s, &(s->LockFile)); } /* Null all structures in case we will be asked for new initialisation */ s->Phone.Data.ModelInfo = NULL; s->Phone.Data.Manufacturer[0] = 0; s->Phone.Data.Model[0] = 0; s->Phone.Data.Version[0] = 0; s->Phone.Data.VerDate[0] = 0; s->Phone.Data.VerNum = 0; return ERR_NONE; }
/** * Opens connection to device and initiates protocol layer. */ GSM_Error GSM_OpenConnection(GSM_StateMachine *s) { GSM_Error error; if (s->CurrentConfig->LockDevice) { error = lock_device(s, s->CurrentConfig->Device, &(s->LockFile)); if (error != ERR_NONE) return error; } /* Irda devices can set now model to some specific and * we don't have to make auto detection later */ error=s->Device.Functions->OpenDevice(s); if (error!=ERR_NONE) { if (s->LockFile != NULL) unlock_device(s, &(s->LockFile)); return error; } s->opened = TRUE; error=s->Protocol.Functions->Initialise(s); if (error!=ERR_NONE) return error; return ERR_NONE; }
/* if a device is dead, use this function to unregister from device list. */ int unregister_device(int i) { IDEV *p; if (dev_list.dev[i].active == 0) return -1; /* 1. clear flags */ if (clear_in_use_flag_for_device(i)) return -2; p = dev_list.dev[i].idev; /* I don't know if it is ok or not. */ /* TOFIX: how to free a structure with mutex ? */ lock_device(p); idev_lock(p); idev_unlock(p); unlock_device(p); /* 2. shut down daemon thread */ if (pthread_cancel(dev_list.dev[i].idev->thread_id)) dm_log(NULL, "\n[VITAL ERROR] thread cancel failed. going on.\n"); /* 3. remove device structure */ dev_list.dev_total--; dev_list.dev[i].active = 0; dev_list.dev[i].during_test = 0; idev_release(p); dev_list.dev[i].idev = NULL; dm_log(NULL, "REMOVED device index %d.", i); /* done */ return 0; }