+int _bt_otp_obj_name_write_cb(char *value, int len)
+{
+ struct object_metadata *object;
+ char *filename;
+ char new_abs_filepath[BT_FILE_PATH_MAX_LEN] = {0, };
+ int ret = BLUETOOTH_ERROR_NONE;
+ FILE *fp = NULL;
+ char err_msg[256] = {0, };
+
+ object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
+ if (!object)
+ return BLUETOOTH_OTP_ERROR_WRITE_REQUEST_REJECTED;
+
+ filename = g_strndup(value, len);
+ snprintf(new_abs_filepath, BT_FILE_PATH_MAX_LEN, "%s%s",
+ directory, filename);
+ BT_DBG("file_path = [%s]", new_abs_filepath);
+
+ fp = fopen(new_abs_filepath, "r");
+ /* fopen succeed means file already exists */
+ if (fp) {
+ ret = BLUETOOTH_OTP_ERROR_OBJECT_NAME_EXISTS;
+ goto fail;
+ }
+
+ if (oacp_create) {
+ struct stat st;
+
+ fp = fopen(new_abs_filepath, "a");
+ if (!fp) {
+ cynara_strerror(errno, err_msg, sizeof(err_msg));
+ BT_ERR("fopen() failed : %s", err_msg);
+ ret = BLUETOOTH_ATT_ERROR_INTERNAL;
+ goto fail;
+ }
+
+ if (stat(new_abs_filepath, &st) == -1) {
+ BT_INFO("stat failed: (%d)\n", errno);
+ ret = BLUETOOTH_ATT_ERROR_INTERNAL;
+ goto fail;
+ }
+
+ object->name = g_strdup(filename);
+ object->first_created = st.st_ctime;
+ object->last_modified = st.st_ctime;
+ object->curr_size = (uint32_t) st.st_size;
+ oacp_create = FALSE;
+ } else {
+ char old_abs_filepath[BT_FILE_PATH_MAX_LEN] = {0, };
+ snprintf(old_abs_filepath, BT_FILE_PATH_MAX_LEN, "%s%s",
+ directory, object->name);
+
+ if (rename(old_abs_filepath, new_abs_filepath)) {
+ ret = BLUETOOTH_ATT_ERROR_INTERNAL;
+ goto fail;
+ }
+ }
+
+ memcpy(object->name, value, len);
+ _bt_otp_set_char_value(otp_object_name_obj_path, value, len);
+
+fail:
+ if (oacp_create)
+ _bt_otp_restore_old_object();
+
+ if (oacp_create_timeout_id > 0)
+ g_source_remove(oacp_create_timeout_id);
+
+ if (fp)
+ fclose(fp);
+
+ g_free(filename);
+ return ret;
+}
+
+int _bt_otp_obj_first_created_write_cb(char *value, int len)
+{
+ struct object_metadata *object;
+ struct tm tm = {0};
+ uint16_t year;
+
+ object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
+ if (!object)
+ return BLUETOOTH_OTP_ERROR_WRITE_REQUEST_REJECTED;
+
+ year = (uint16_t)(value[1] & 0xFF) << 8 |
+ (uint16_t)(value[0] & 0xFF);
+ tm.tm_year = year-1900;
+ tm.tm_mon = value[2] & 0xFF;
+ tm.tm_mon = tm.tm_mon-1;
+ tm.tm_mday = value[3] & 0xFF;
+ tm.tm_hour = value[4] & 0xFF;
+ tm.tm_min = value[5] & 0xFF;
+ tm.tm_sec = value[6] & 0xFF;
+
+ object->first_created = mktime(&tm);
+ _bt_otp_set_char_value(otp_object_first_created_obj_path, value, len);
+
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_otp_obj_last_modified_write_cb(char *value, int len)
+{
+ struct object_metadata *object;
+ struct tm tm = {0};
+ uint16_t year;
+
+ object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
+ if (!object)
+ return BLUETOOTH_OTP_ERROR_WRITE_REQUEST_REJECTED;
+
+ year = (uint16_t)(value[1] & 0xFF) << 8 |
+ (uint16_t)(value[0] & 0xFF);
+ tm.tm_year = year-1900;
+ tm.tm_mon = value[2] & 0xFF;
+ tm.tm_mon = tm.tm_mon-1;
+ tm.tm_mday = value[3] & 0xFF;
+ tm.tm_hour = value[4] & 0xFF;
+ tm.tm_min = value[5] & 0xFF;
+ tm.tm_sec = value[6] & 0xFF;
+
+ object->last_modified = mktime(&tm);
+ _bt_otp_set_char_value(otp_object_last_modified_obj_path, value, len);
+
+ return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_otp_obj_props_write_cb(char *value, int len)
+{
+ struct object_metadata *object;
+ uint32_t props;
+
+ /* Any attempt to write RFU bits is error */
+ if (value[1] || value[2] || value[3])
+ return BLUETOOTH_OTP_ERROR_WRITE_REQUEST_REJECTED;
+
+ object = (struct object_metadata *) g_slist_nth_data(otp_object_list, curr_obj_index);
+ if (!object)
+ return BLUETOOTH_OTP_ERROR_WRITE_REQUEST_REJECTED;
+
+ props = (uint32_t)(value[3] & 0xFF) << 24 |
+ (uint32_t)(value[2] & 0xFF) << 16 |
+ (uint32_t)(value[1] & 0xFF) << 8 |
+ (uint32_t)(value[0] & 0xFF);
+
+ object->props = props;
+ _bt_otp_set_char_value(otp_object_prop_obj_path, value, len);
+
+ return BLUETOOTH_ERROR_NONE;
+}
+