mconnect - KDE Connect protocol implementation in Vala/C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.9 KiB

  1. // Licensed under the Apache License, Version 2.0 (the "License");
  2. // you may not use this file except in compliance with the License.
  3. // You may obtain a copy of the License at
  4. //
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS,
  9. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. // See the License for the specific language governing permissions and
  11. // limitations under the License.
  12. package main
  13. import (
  14. "context"
  15. "fmt"
  16. "os"
  17. "github.com/jessevdk/go-flags"
  18. "github.com/bboozzoo/mconnect/logger"
  19. "github.com/bboozzoo/mconnect/protocol"
  20. "github.com/bboozzoo/mconnect/protocol/packet"
  21. uflags "github.com/bboozzoo/mconnect/utils/flags"
  22. )
  23. var (
  24. Stderr = os.Stderr
  25. Stdout = os.Stdout
  26. )
  27. func main() {
  28. var opts struct {
  29. Debug bool `short:"d" long:"debug" description:"Show debugging information"`
  30. Address string `short:"a" long:"address" description:"Address of remote device"`
  31. }
  32. _, err := flags.ParseArgs(&opts, os.Args)
  33. if err != nil {
  34. uflags.HandleFlagsError(err)
  35. }
  36. ctx := context.Background()
  37. ctx = logger.WithContext(ctx, logger.New())
  38. log := logger.FromContext(ctx)
  39. log.SetLevel(logger.ErrorLevel)
  40. if opts.Debug {
  41. log.SetLevel(logger.DebugLevel)
  42. }
  43. hostname, err := os.Hostname()
  44. if err != nil {
  45. fmt.Fprintf(Stderr, "error: failed to obtain hostname: %v\n",
  46. err)
  47. os.Exit(1)
  48. }
  49. conf := protocol.Configuration{
  50. Identity: &packet.Identity{
  51. DeviceId: "mconnect-" + hostname,
  52. DeviceName: hostname,
  53. DeviceType: "computer",
  54. ProtocolVersion: 7,
  55. TcpPort: 1716,
  56. },
  57. }
  58. conn, err := protocol.Dial(ctx, opts.Address, &conf)
  59. if err != nil {
  60. log.Errorf("connection failed: %v", err)
  61. os.Exit(1)
  62. }
  63. defer conn.Close()
  64. }